inVad3r inVad3r - 4 months ago 10
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
DECLARE @calcDate date = '7/7/2016'

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