clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 1811878 fiddles created (25304 in the last week).

CREATE TABLE Dates ( ID INT IDENTITY, StartDate DATE, EndDate DATE )
 hidden batch(es)


INSERT INTO Dates (StartDate, EndDate) VALUES ('2016-06-09', '2019-12-31'), ('2016-06-09', '2019-01-30'), ('2019-02-09', '2019-07-30')
3 rows affected
 hidden batch(es)


SELECT ID, StartDate, EndDate, (CASE WHEN StartDate < '2019-01-01' THEN DATEDIFF(DAY, '2019-01-01', EndDate) ELSE DATEDIFF(DAY, StartDate, EndDate) END)+1 AS DaysInRange FROM Dates
ID StartDate EndDate DaysInRange
1 2016-06-09 2019-12-31 365
2 2016-06-09 2019-01-30 30
3 2019-02-09 2019-07-30 172
 hidden batch(es)