Extract Date Part (or beginning of the day) for Datetime Data Type with T-SQL


  
---SQL SEver 2012, 2014



SELECT 
--Use GETDATE()
dateadd(day,day(getdate()), EOMONTH(getdate(),-1)) 
, Cast(getdate() as Date)
, Convert(Date,getdate())
,datefromparts(Year(getdate()),month(getdate()),day(getdate()))
,dateadd(day,datediff(day,0,getdate()),0)
,Dateadd(day,datepart(dayofyear,getdate())-1,dateadd(year,datediff(year,0,getdate()),0))
,DATETIMEFROMPARTS(Year(getdate()),month(getdate()),day(getdate()),0,0,0,0)
,SMALLDATETIMEFROMPARTS(Year(getdate()),month(getdate()),day(getdate()),0,0)
,DATETIME2FROMPARTS(Year(getdate()),month(getdate()),day(getdate()),0,0,0,0,0)


--Use current_timestamp
,dateadd(day,day(current_timestamp), eomonth(current_timestamp,-1)) 
, Cast(current_timestamp as Date)
, Convert(Date,current_timestamp)
,datefromparts(Year(current_timestamp),month(current_timestamp),day(current_timestamp))
,dateadd(day,datediff(day,0,current_timestamp),0)
,Dateadd(day,datepart(dayofyear,current_timestamp)-1,dateadd(year,datediff(year,0,current_timestamp),0))
,SMALLDATETIMEFROMPARTS(Year(current_timestamp),month(current_timestamp),day(current_timestamp),0,0)
,DATETIMEFROMPARTS(Year(current_timestamp),month(current_timestamp),day(current_timestamp),0,0,0,0)
,DATETIME2FROMPARTS(Year(current_timestamp),month(current_timestamp),day(current_timestamp),0,0,0,0,0)



 
Advertisements


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s