Sunday, 3 December 2006

TSQL Date Manipulation

Negate Time Part (Midnight for today)
SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)


Monday of the Current Week
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)


First Day of Month
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()), 0)


Last Day of Month
SELECT DATEADD(dd,-1,DATEADD(mm, DATEDIFF(mm,0,GETDATE())+1, 0))


First Day of the Year
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)


Last Day of Year
SELECT DATEADD(dd,-1,DATEADD(yy, DATEDIFF(yy,0,GETDATE())+1, 0))

No comments: