sql server结合date_time和smallint列以12小时格式派生datetime列

时间:2017-07-21 07:43:37

标签: sql-server

我有一个date_time列和hour_ending列,如下所示。如何将它们连接在一起以12小时日期格式派生date_time列。我的要求是使用date_time作为连接键

将表A与表B连接起来

表A

DATE                HOUR_ENDING
---             ----------

8/31/2016 12:00:00.000 AM   1
8/31/2016 12:00:00.000 AM   2
8/31/2016 12:00:00.000 AM   3
8/31/2016 12:00:00.000 AM   4
8/31/2016 12:00:00.000 AM   5
8/31/2016 12:00:00.000 AM   6
8/31/2016 12:00:00.000 AM   7
8/31/2016 12:00:00.000 AM   8
8/31/2016 12:00:00.000 AM   9
8/31/2016 12:00:00.000 AM   10
8/31/2016 12:00:00.000 AM   11
8/31/2016 12:00:00.000 AM   12
8/31/2016 12:00:00.000 AM   13
8/31/2016 12:00:00.000 AM   14
8/31/2016 12:00:00.000 AM   15
8/31/2016 12:00:00.000 AM   16
8/31/2016 12:00:00.000 AM   17
8/31/2016 12:00:00.000 AM   18
8/31/2016 12:00:00.000 AM   19
8/31/2016 12:00:00.000 AM   20
8/31/2016 12:00:00.000 AM   21
8/31/2016 12:00:00.000 AM   22
8/31/2016 12:00:00.000 AM   23
8/31/2016 12:00:00.000 AM   24

表B (我需要表A这样)

8/31/2013 12:00:00 AM
8/31/2013 1:00:00 AM
8/31/2013 2:00:00 AM
8/31/2013 3:00:00 AM
8/31/2013 4:00:00 AM
8/31/2013 5:00:00 AM
8/31/2013 6:00:00 AM
8/31/2013 7:00:00 AM
8/31/2013 8:00:00 AM
8/31/2013 9:00:00 AM
8/31/2013 10:00:00 AM
8/31/2013 11:00:00 AM
8/31/2013 12:00:00 PM
8/31/2013 1:00:00 PM
8/31/2013 2:00:00 PM
8/31/2013 3:00:00 PM
8/31/2013 4:00:00 PM
8/31/2013 5:00:00 PM
8/31/2013 6:00:00 PM
8/31/2013 7:00:00 PM
8/31/2013 8:00:00 PM
8/31/2013 9:00:00 PM
8/31/2013 10:00:00 PM
8/31/2013 11:00:00 PM
9/1/2013 12:00:00 AM

1 个答案:

答案 0 :(得分:0)

您可以使用DATEADD()使用小时偏移来调整A表中的日期。然后,使用此调整后的时间戳将表A连接到B.

SELECT *
FROM tableA a
INNER JOIN tableB b
    ON DATEADD(HOUR, a.HOUR_ENDING, a.DATE) = b.DATE

顺便说一下,您应该考虑更改表格设计,以便将日期和时间一起存储在一个列中。