Extracting hours from a DateTime (SQL Server 2005)

后端 未结 11 547
野趣味
野趣味 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 04:01

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

提交回复
热议问题