shell脚本,用于打印给定日期的所有半小时块的列表

nuypyhwy  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(277)

我想为mysql查询中传递的任何给定日期生成下面的列表。
示例:给定日期为“2018-08-28”
我需要输出为

between '2018-08-28 00:00:00' and '2018-08-28 00:29:59'
between '2018-08-28 00:30:00' and '2018-08-28 00:59:59'
between '2018-08-28 1:00:00' and '2018-08-28 1:29:59'
between '2018-08-28 1:30:00' and '2018-08-28 1:59:59'
-
-
-
-

between '2018-08-28 23:00:00' and '2018-08-28 23:29:59'
between '2018-08-28 23:30:00' and '2018-08-28 23:59:59'
ktecyv1j

ktecyv1j1#

DECLARE @dtStart AS DATETIME ='20180828 00:00:00' 
        ,@dtEnd AS DATETIME = '20180828 23:59:59'
        ,@iInterval AS INT = 30;  --30 min interval

WITH aCTE
AS(
    SELECT 
        @dtStart AS StartDateTime,
        DATEADD(MINUTE,@iInterval,@dtStart) AS EndDateTime
    UNION ALL
    SELECT 
        DATEADD(MINUTE,@iInterval,StartDateTime),
        DATEADD(MINUTE,@iInterval,EndDateTime)
    FROM aCTE
    WHERE
        DATEADD(MINUTE,@iInterval,EndDateTime) <= @dtEnd
)

SELECT 

    CONVERT(VARCHAR(10),StartDateTime,108) 
    + ' ' + RIGHT(CONVERT(VARCHAR(30), StartDateTime, 9), 2) 
    + ' - ' +

    CONVERT(VARCHAR(10),EndDateTime,108) 
    + ' ' + RIGHT(CONVERT(VARCHAR(30), EndDateTime, 9), 2) AS Result
FROM aCTE

试试这个,如果你发现任何问题就回复我。

相关问题