SQL Server dates

 

to get different type of dates based on a date.

select
getdate()
, cast(cast(getdate() as date) as datetime) today_00hrs
, dateadd(dd,-1, cast(getdate() as date)) as yesterday_date
, cast(dateadd(dd,-1, cast(getdate() as date)) as datetime) as yesterday_00hrs
, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear
, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as StartOfMonth
, (DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)) StartOfLastMonth
, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-3, 0) as Startof3monthsback
, DATEPART(week, GETDATE())weekofdate
, DATEADD(yy,-1,getdate() ) LastYearSameDateTime

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s