查询将UTC转换为CST

时间:2018-01-22 20:46:49

标签: sql sql-server datetime

我正在使用SQL Server在24小时内计算行数。我已经完成了,但我无法弄清楚如何将UTC时间转换为数据库存储的CST

则...

如何将24小时分解为每小时行数为SUM或计数的24 x 1小时块?

将UTC TIME转换为CST?

查询?

- 那么

Select COUNT (*) AS Total
From readmodels.Database
Where
    Timestamp >= '2018-01-18' AND 
    Timestamp <= '2018-01-19'

- 然后将计数分解为24 - 1小时块

2 个答案:

答案 0 :(得分:0)

如果您只需要从UTC转换为CST。您可以在查询中使用DATEADD(小时,-6,时间戳)。

e.g。

Select COUNT(*) as count, DATEPART(year, DATEADD(hour, -6, Timestamp)) as year, DATEPART(month, DATEADD(hour, -6, Timestamp)) as month, DATEPART(day, DATEADD(hour, -6, Timestamp)) as day, DATEPART(hour, DATEADD(hour, -6, Timestamp)) as hour
From readmodels.Database
Where
DATEADD(hour, -6, Timestamp) >= '2018-01-18' AND 
DATEADD(hour, -6, Timestamp) <= '2018-01-19'
Group by DATEPART(year, DATEADD(hour, -6, Timestamp)), DATEPART(month, DATEADD(hour, -6, Timestamp)), DATEPART(day, DATEADD(hour, -6, Timestamp)), DATEPART(hour, DATEADD(hour, -6, Timestamp))

答案 1 :(得分:0)

- 这就是我最终使用的

SELECT  dateadd(hour, datediff(hour, 0, TimeStamp), 0) as TimeStampHour_CST, Count(*) As Total_Per_Hour
FROM    readmodels.database
WHERE   Timestamp >= '2018-01-17' AND 
        Timestamp <= '2018-01-18'
GROUP BY dateadd(hour, datediff(hour, 0, TimeStamp), 0)
ORDER BY dateadd(hour, datediff(hour, 0, TimeStamp), 0);