inVad3r inVad3r - 1 year ago 88
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download