Extracting hours from a DateTime (SQL Server 2005)

SqlTsqlDatetimeSql Server-2005Hour

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

SELECT DATEPART(HOUR, GETDATE());

DATEPART documentation

Solution 2 - Sql

... 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")

Solution 3 - Sql

Use datepart.

E.g.:

datepart(hh, date)

Solution 4 - Sql

try this one too:

   DATEPART(HOUR,GETDATE()) 

Solution 5 - Sql

The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

datepart    ***Abbreviation

year        ***yy, yyyy 
quarter     ***qq, q 
month       ***mm, m 
dayofyear   ***dy, y 
day         ***dd, d 
week        ***wk, ww 
weekday     ***dw, w 
hour        ***hh 
minute      ***mi, n 
second      ***ss, s 
millisecond ***ms 
microsecond ***mcs 
nanosecond  ***ns 

Example

select * 
from table001
where datepart(hh,datetime) like 23

Solution 6 - Sql

DATEPART(HOUR, [date]) returns the hour in military time ( 00 to 23 ) If you want 1AM, 3PM etc, you need to case it out:

SELECT Run_Time_Hour =
CASE DATEPART(HOUR, R.date_schedule)
	WHEN 0 THEN  '12AM'
	WHEN 1 THEN   '1AM'
	WHEN 2 THEN   '2AM'
	WHEN 3 THEN   '3AM'
	WHEN 4 THEN   '4AM'
	WHEN 5 THEN   '5AM'
	WHEN 6 THEN   '6AM'
	WHEN 7 THEN   '7AM'
	WHEN 8 THEN   '8AM'
	WHEN 9 THEN   '9AM'
	WHEN 10 THEN '10AM'
	WHEN 11 THEN '11AM'
	WHEN 12 THEN '12PM'
	ELSE CONVERT(varchar, DATEPART(HOUR, R.date_schedule)-12) + 'PM'
END
FROM
	dbo.ARCHIVE_RUN_SCHEDULE R

Solution 7 - Sql

Try this one too:

SELECT CONVERT(CHAR(8),GETDATE(),108)

Solution 8 - Sql

select case when [am or _pm] ='PM' and datepart(HOUR,time_received)<>12 
           then dateadd(hour,12,time_received) 
           else time_received 
       END 
from table

works

Solution 9 - Sql

> I can't extract hours, with HOUR(Date())

There is a way to call HOUR (I would not recommend to use it though because there is DATEPART function) using ODBC Scalar Functions:

SELECT {fn HOUR(GETDATE())} AS hour

LiveDemo

Solution 10 - Sql

To include AM / PM - use the below:

SELECT 
  concat(case when datepart(hour,getdate()) % 12 = 0 then 12 
              else datepart(hour,getdate()) % 12 end,
	     case when datepart(hour,getdate()) < 12 then ' AM' 
              else ' PM' end
        )

Solution 11 - Sql

select convert(time,GETDATE())

Solution 12 - Sql

you must use datepart()

like 


datepart(hour , getdate())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEfeView Question on Stackoverflow
Solution 1 - SqlDave MarkleView Answer on Stackoverflow
Solution 2 - SqlMilanView Answer on Stackoverflow
Solution 3 - SqlAdrian GodongView Answer on Stackoverflow
Solution 4 - Sqluser594166View Answer on Stackoverflow
Solution 5 - SqlHamid NadiView Answer on Stackoverflow
Solution 6 - SqlrelgremView Answer on Stackoverflow
Solution 7 - Sqlarmando rodriguezView Answer on Stackoverflow
Solution 8 - SqlDaniView Answer on Stackoverflow
Solution 9 - SqlLukasz SzozdaView Answer on Stackoverflow
Solution 10 - SqlShawnView Answer on Stackoverflow
Solution 11 - SqlR.AlonsoView Answer on Stackoverflow
Solution 12 - SqlFredView Answer on Stackoverflow