在SQL Server中,我想从datetime值获取一年中的小时(从1到8760)。知道怎么做到这一点?非常感谢!
示例(日期时间格式为yyyy-mm-dd-hh-mm-ss)
Input: datetime => Output: hour_of_year
----------------------------------------
2015-01-01 01:00:00 => 1
2015-01-01 02:00:00 => 2
2015-01-02 01:00:00 => 25
2015-02-15 08:00:00 => 1088
2015-12-31 22:00:00 => 8758
2015-12-31 23:00:00 => 8759
答案 0 :(得分:3)
您可以使用DATEPART
:
SELECT DATEPART(DAYOFYEAR,GETDATE())*24 + DATEPART(HOUR,GETDATE())
答案 1 :(得分:0)
SELECT DATEDIFF ( hour, DATEADD(yy, DATEDIFF(yy,0,GETDATE()), 0), GETDATE())