设置另一个工作时间而不是时间

时间:2019-02-19 12:40:49

标签: sql-server date datetime sql-server-2017

我有一个DateTime维度,其日期如下:

Datetime                      Date hour
--------------------------------------
2026-01-01 00:00:00.0000000       24
2025-12-31 23:00:00.0000000       23
2025-12-31 22:00:00.0000000       22
2025-12-31 21:00:00.0000000       21
2025-12-31 20:00:00.0000000       20
2025-12-31 19:00:00.0000000       19
2025-12-31 18:00:00.0000000       18
2025-12-31 17:00:00.0000000       17
2025-12-31 16:00:00.0000000       16
2025-12-31 15:00:00.0000000       15
2025-12-31 14:00:00.0000000       14
2025-12-31 13:00:00.0000000       13
2025-12-31 12:00:00.0000000       12
2025-12-31 11:00:00.0000000       11
2025-12-31 10:00:00.0000000       10
2025-12-31 09:00:00.0000000       9
2025-12-31 08:00:00.0000000       8
2025-12-31 07:00:00.0000000       7
2025-12-31 06:00:00.0000000       6
2025-12-31 05:00:00.0000000       5
2025-12-31 04:00:00.0000000       4
2025-12-31 03:00:00.0000000       3
2025-12-31 02:00:00.0000000       2
2025-12-31 01:00:00.0000000       1

由于系统时间,我需要我的时间从6点开始,但是我该怎么做?我可以用硬编码的值创建一个临时表并加入,但是必须有一个更简单的方法吗?

所需结果:

Datetime                      Date hour
--------------------------------------
2026-01-01 00:00:00.0000000       19
2025-12-31 23:00:00.0000000       18
2025-12-31 22:00:00.0000000       17
2025-12-31 21:00:00.0000000       16
2025-12-31 20:00:00.0000000       15
2025-12-31 19:00:00.0000000       14
2025-12-31 18:00:00.0000000       13
2025-12-31 17:00:00.0000000       12
2025-12-31 16:00:00.0000000       11
2025-12-31 15:00:00.0000000       10
2025-12-31 14:00:00.0000000       9
2025-12-31 13:00:00.0000000       8
2025-12-31 12:00:00.0000000       7
2025-12-31 11:00:00.0000000       6
2025-12-31 10:00:00.0000000       5
2025-12-31 09:00:00.0000000       4
2025-12-31 08:00:00.0000000       3
2025-12-31 07:00:00.0000000       2
2025-12-31 06:00:00.0000000       1
2025-12-31 05:00:00.0000000       24
2025-12-31 04:00:00.0000000       23
2025-12-31 03:00:00.0000000       22
2025-12-31 02:00:00.0000000       21
2025-12-31 01:00:00.0000000       20
  

关于我为什么不持有DateDimension和   TimeDimension。因为我有很多,所以这不能满足我的要求   关于日期时间维度的更多业务规则

2 个答案:

答案 0 :(得分:1)

鉴于您的[Date hour]列已被计算,您可以尝试使用模数进行更新:

UPDATE yourTable
SET [Date hour] = 1 + ([Date hour] + 18) % 24;

Demo

更好的长期解决方案可能是根据原始日期时间信息将日期小时列设置为计算列。通过@Larnu找出答案,这是一种解决方法。

答案 1 :(得分:1)

一个猜测...

UPDATE YourTable
SET [Date hour] = IIF(DATEPART(HOUR,DATEADD(HOUR, -5, [datetime])) = 0,24,DATEPART(HOUR,DATEADD(HOUR, -5, [datetime]));