Examples of how to Calculate Different SQL Server Dates

First Day of Month
select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
Monday of the Current Week
select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)

set DATEFIRST 1

select DATEADD(dd, 1 – DATEPART(dw, getdate()), getdate())

First Day of the Year
select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
First Day of the Quarter
select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
Midnight for the Current Day
Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
Last Day of Prior Month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()Â ), 0))
Last Day of Prior Year
select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()Â ), 0))
Last Day of Current Month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate()Â )+1, 0))
Last Day of Current Year
Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()Â )+1, 0))
First Monday of the Month
select DATEADD(wk, DATEDIFF(wk,0,
           dateadd(dd,6-datepart(day,getdate()),getdate())
                              ), 0)    Â

Â

Â

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *