MS SQL Tips & Tricks

How to get time 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 @cDateTime-dateadd(dd,0, datediff(dd,0,@cDateTime))

... and the second

declare @cDateTime datetime;

set @cDateTime = GetDate()

select	@cDateTime-(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

27th August 2021

http://buytadalafshop.com/ - purchasing cialis online

ceagods

8th September 2021

low cost alternatives and viagra

Invorne

12th September 2021

https://buystromectolon.com/ - Stromectol

uribete