如何在sql sever 2014中以15分钟的间隔打破日期时间

时间:2018-03-13 13:05:48

标签: sql sql-server sql-server-2014

我已经在15分钟的时间间隔内根据“开始日期时间”拆分了以下查询,但此查询未提供确切的结果集 正如我所料。 下面是我想要执行的查询示例。

select Date_Stamp,
 Case when substring(convert(char(8),starttime,114), 1, 8) between '12:00:01 AM'and '12:15:00 AM' then '0015'
 when substring(convert(char(8),starttime,114), 1, 8) between '12:15:01 AM'and '12:30:00 AM' then '0030'
 when substring(convert(char(8),starttime,114), 1, 8) between '12:30:01 AM'and '12:45:00 AM' then '0045'
 when substring(convert(char(8),starttime,114), 1, 8) between '12:45:01 AM'and '01:00:00 AM' then '0100'

,我希望结果为

Date    Need result set
12:01 AM     '0015'
'12:15:01   '0030'
'12:30:01   '0045'
'12:45:01   '0100'
'01:00:01   '0115'
'01:15:01   '0130'
'01:30:01   '0145'
'01:45:01   '0200'
'02:00:01   '0215'
'02:15:01   '0230'
'02:30:01   '0245'
3:00:00 '    '0015'
'12:30:00   '0030'
'12:45:00   '0045'
'01:00:00   '0100'
'01:15:00   '0115'
'01:30:00   '0130'
'01:45:00   '0145'
'02:00:00   '0200'
'02:15:00   '0215'
'02:30:00   '0230'
'02:45:00   '0245'

3 个答案:

答案 0 :(得分:2)

只需使用列名

更改@starttime即可
DECLARE @starttime datetime = getdate()
SELECT CONCAT(CASE  WHEN DATEPART(HH, @starttime) <= 9
                    THEN '00'+ CAST(DATEPART(HH, @starttime) AS VARCHAR(2))
                    ELSE '0'+CAST(DATEPART(HH, @STARTTIME) AS VARCHAR(2))
              END,
              CASE  WHEN DATEPART(MINUTE, @STARTTIME) BETWEEN 1 AND 15
                    THEN 15
                    WHEN DATEPART(MINUTE, @STARTTIME) BETWEEN 16 AND 30
                    THEN 30
                    WHEN DATEPART(MINUTE, @STARTTIME) BETWEEN 31 AND 45
                    THEN 45
                    WHEN DATEPART(MINUTE, @STARTTIME) BETWEEN 46 AND 59 OR DATEPART(MINUTE, @STARTTIME) = 0
                    THEN 00
              END)

答案 1 :(得分:1)

您可以使用此日期生成器:

DEMO

    DECLARE @Break INT = 15 
    ;WITH Numbers (n) as 
(
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1
    FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0)) a(n)
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d(n)
)
    ,Dates as
    (
    SELECT dt
    FROM Numbers 
        CROSS APPLY 
            (
            VALUES (DATEADD(MINUTE , n, CAST(CAST(GETDATE() AS DATE) AS DATETIME)))
            ) X(Dt)
     WHERE N % @Break = 0 
     AND CAST(DT AS DATE) = CAST(GETDATE() AS DATE) --Only for today's date
    )
    SELECT CONVERT(VARCHAR(10),Dt,108) [Time] , REPLACE(CONVERT(VARCHAR(5),ISNULL(Lead(Dt) OVER (ORDER BY Dt) , DATEADD(MINUTE,@Break,Dt)),108), ':','') Grp 
    FROM Dates 

答案 2 :(得分:0)

您似乎正在使用日期时间,并且只占用了时间的子字符串。无法将字符串与时间进行比较,而不会将其转换为时间数据类型。

例如:

    DECLARE @mytable TABLE (starttime datetime)
    INSERT INTO @mytable VALUES ('2018-03-13 00:00:01'), ('2018-03-15 00:00:01')
    SELECT * FROM @mytable

select CAST(starttime as time(0)) AS [thetime],
 Case when CAST(starttime as time) between '12:00:01 AM'and '12:15:00 AM' then '0015'
 when CAST(starttime as time) between '12:15:01 AM'and '12:30:00 AM' then '0030'
 when CAST(starttime as time) between '12:30:01 AM'and '12:45:00 AM' then '0045'
 when CAST(starttime as time) between '12:45:01 AM'and '01:00:00 AM' then '0100'
 END AS [Interval]
FROM @mytable

产地:

thetime     Interval
00:00:01    0015
00:15:01    0030