Extracting hours from a DateTime (SQL Server 2005)
I can extract the month and day by using Day(Date())
, Month(Date())
. I can't extract hours, with HOUR(Date())
. I get the following error.
'HOUR' is not a recognized built-in function name.
How can I extract hours?
Solution 1:
SELECT DATEPART(HOUR, GETDATE());
DATEPART
documentation
Solution 2:
... you can use it on any granularity type i.e.:
DATEPART(YEAR, [date])
DATEPART(MONTH, [date])
DATEPART(DAY, [date])
DATEPART(HOUR, [date])
DATEPART(MINUTE, [date])
(note: I like the [ ] around the date reserved word though. Of course that's in case your column with timestamp is labeled "date")