如何从仅仅日期列生成date_time。例如,我有8/31/2013,我想按顺序排列从1 ... 24小时结束的小时
(我需要表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
答案 0 :(得分:0)
以下代码:
DECLARE @Date1 DATETIME, @Date2 DATETIME
SET @Date1 = '2013-08-30 11:00:00'
SET @Date2 = '2013-08-30 23:00:00'
SELECT DATEADD(HOUR,number+1,@Date1) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(HOUR,number+1,@Date1) < @Date2
结果为:
这是你在找什么?我相信你可以在表示层中找出AM/PM
。
修改强>
如果您需要AM/PM
时间,以下内容适合您:
DECLARE @Date1 DATETIME, @Date2 DATETIME
SET @Date1 = '2013-08-30 11:00:00'
SET @Date2 = '2013-08-30 23:00:00'
SELECT CONVERT( VARCHAR, DATEADD(HOUR,number+1,@Date1), 101)+' '+LTRIM(RIGHT(CONVERT(VARCHAR, DATEADD(HOUR,number+1,@Date1), 100), 7)) AS [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(HOUR,number+1,@Date1) < @Date2