Extracting hours from a DateTime (SQL Server 2005)

Viewed 717100

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?

12 Answers

Try this one too:

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

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
        )
Related