Extracting hours from a DateTime (SQL Server 2005)

后端 未结 11 501
野趣味
野趣味 2020-12-01 03:21

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 er

相关标签:
11条回答
  • 2020-12-01 03:39

    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

    0 讨论(0)
  • 2020-12-01 03:48

    Try this one too:

    SELECT CONVERT(CHAR(8),GETDATE(),108)
    
    0 讨论(0)
  • 2020-12-01 03:49

    Use datepart.

    E.g.:

    datepart(hh, date)
    
    0 讨论(0)
  • 2020-12-01 03:49

    you must use datepart()

    like 
    
    
    datepart(hour , getdate())
    
    0 讨论(0)
  • 2020-12-01 03:51
    select convert(time,GETDATE())
    
    0 讨论(0)
  • 2020-12-01 03:53
    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

    0 讨论(0)
提交回复
热议问题