SQL statement for calculating the number of days of the year where a given date is located declare @ datedatetime; set @ dategetdate () -- number of days between the first day of the current year and the first day of the next year selectdatediff (day, dateadd (year, datediff (year, 0, @ date), 0), dateadd (year, 1 + datediff (year, 0, @ date), 0 )) -- SQL statement used to calculate the number of days of the year of a given date
Declare @ date datetime;
Set @ date = getdate ()
-- The number of days between the first day of the current year and the first day of the next year
Select datediff (day, dateadd (year, datediff (year, 0, @ date), 0), dateadd (year, 1 + datediff (year, 0, @ date ), 0 ))
-- Simplify with variables
Select @ date = dateadd (year, datediff (year, 0, @ date), 0) -- the first day of the year
Select datediff (day, @ date, dateadd (year, 1, @ date ))
Go