inVad3r inVad3r - 1 year ago 66
SQL Question

SQL - Modifying Dates

What is the best way to take a date variable in SQL and set it to Beginning of Year and End of Year?

declare @calcDate date = '7/7/2016'

How do I get '1/1/2016' and '12/31/2016' using @calcDate?

Answer Source
DECLARE @calcDate date = '7/7/2016'

DATEADD(yy, DATEDIFF(yy,0,@calcDate), 0) AS BeginningOfYear,
DATEADD(yy, DATEDIFF(yy,0,@calcDate) + 1, -1) AS EndOfYear