MS SQL Tips & Tricks

How to get date part of datetime value

Very common problem is how to get date part of DATETIME column. The simplest way is: You can also use one of this methods:

First method

declare @cDateTime datetime;

set @cDateTime = GetDate()

select dateadd(dd,0, datediff(dd,0,@cDateTime))

... and the second

declare @cDateTime datetime;

set @cDateTime = GetDate()

select	(CAST(	
	CEILING( 
		CAST( 
			CAST(@cDateTime AS DATETIME) AS float
			)
		)AS DATETIME)-
	(CEILING( 
		CAST( 
			CAST(@cDateTime AS DATETIME) AS float
			)
		)
	- FLOOR( 
	CAST( 
		CAST(@cDateTime AS DATETIME)as float
		)
	)
)
Download SQL script

Learn more tricks

Add Comment

Comments