December 8, 2011 at 8:00 am
Hi, I want to return only the hour of a time ie 17:00 should return 17
The code below selects the time from a date and then rounds up or down to the nearest hour.
SELECT CAST(DATEADD(hour,DATEDIFF(hour,0,DATEADD(minute,30,GETDATE())),0) AS TIME(0))
I tried to add the SUBSTRING function to this but to no avail.
Any suggestions?
Thanks!!
December 8, 2011 at 8:05 am
convert the date right away to time, and then you are working with hours from 0 to 23:59:59;
so when you round the TIMe, it's between 0 and 23:
--returns 10 AS INTEGER
SELECT DATEDIFF(hour,0,DATEADD(minute,30,convert(time,GETDATE())))
Lowell
December 8, 2011 at 8:08 am
Perfect! Thank-you for your prompt response 🙂
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply