如何在sql server中显示时间间隔

时间:2015-06-18 10:49:51

标签: sql-server

我有两个存储时间范围的变量。 我必须以这种格式显示时间间隔

09:00:00  -- 09:30:00
09:30:00  -- 10:00:00
10:00:00  -- 10:30:00
10:30:00  -- 11:00:00

UpTO

05:00:00 -- 05:30:00

这是我的代码

DECLARE @StartTime DATETIME
DECLARE @InvTime DATETIME
DECLARE @EndTime DATETIME

SET @StartTime='09:00:00'
SET @EndTime='17:30:00'

WHILE @StartTime < @EndTime
BEGIN

    SET @InvTime = DATEADD(MINUTE, 30, @StartTime)

    PRINT CONVERT(char(8), @StartTime, 108)+ '  ' + CONVERT(char(8), @InvTime, 108) 
    SET @StartTime = DATEADD(MINUTE, 30, @StartTime)
END

无论如何,我的代码正在运行,但正在寻找通过任何其他最佳方式实现它的建议。

我的工作完成了。完整的SQL如下

DECLARE     @StartTime datetime = '2015-06-09 09:00:00',
            @EndTime datetime = '2015-06-09 18:00:00',
            @Interval int = 30 -- this can be changed.

DECLARE     @PartyIntervals TABLE (
            Party1Name      VARCHAR(500),
            StartRange      DATETIME,
            EndRange        DATETIME) 

DECLARE     @CSVIntervals   TABLE (
            Party1Name      VARCHAR(500),
            Incoming        INT,
            StartRange      DATETIME) 

;WITH cSequence AS
(
    SELECT
       @StartTime AS StartRange, 
       DATEADD(MINUTE, @Interval, @StartTime) AS EndRange
    UNION ALL
    SELECT
      EndRange, 
      DATEADD(MINUTE, @Interval, EndRange)
    FROM cSequence 
    WHERE DATEADD(MINUTE, @Interval, EndRange) < @EndTime
)


INSERT INTO @PartyIntervals
SELECT      Party1Name, 
            StartRange,
            EndRange 
FROM (SELECT DISTINCT Party1Name FROM tridip_Interval) A, cSequence ORDER BY Party1Name;
--SELECT * FROM @PartyIntervals ORDER BY Party1Name

INSERT INTO @CSVIntervals
SELECT  Party1Name,
        count(*) as incoming,
        CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM    tridip_Interval
WHERE   direction='I'
GROUP   BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name

SELECT  P.Party1Name,
        P.StartRange,
        P.EndRange,
        IsNull(Incoming , 0) AS Incoming
FROM    @PartyIntervals P LEFT OUTER JOIN 
        @CSVIntervals A ON  A.Party1Name = P.Party1Name AND P.StartRange = A.StartRange
ORDER BY  P.Party1Name,P.StartRange

2 个答案:

答案 0 :(得分:3)

正如您的代码在这里工作的另一种方法是创建cte,如下所示:

DECLARE @StartTime DATETIME, @EndTime DATETIME

SELECT @StartTime = '09:00:00'
SELECT @EndTime = '17:30:00'


;WITH interval_cte(StartTime, EndTime) AS
(
   SELECT @StartTime StartTime, DATEADD(mi, 30, @StartTime) EndTime

   UNION ALL

   SELECT EndTime, DATEADD(mi, 30, EndTime)  FROM interval_cte
   WHERE EndTime < @EndTime

)  
SELECT * FROM interval_cte

<强> SQL FIDDLE DEMO

仅获取时间间隔,即没有日期部分尝试如下:

DECLARE @StartTime DATETIME, @EndTime DATETIME

SELECT @StartTime = '09:00:00'
SELECT @EndTime = '17:30:00'


;WITH time_cte(StartTime, EndTime) AS
(
   SELECT CONVERT(varchar,@StartTime,108) StartTime, CONVERT(varchar,(DATEADD(mi, 30, @StartTime)),108) EndTime

   UNION ALL

   SELECT CONVERT(varchar,EndTime,108), CONVERT(varchar,(DATEADD(mi, 30, EndTime)),108)  FROM time_cte
   WHERE EndTime < @EndTime

)  
SELECT * FROM time_cte

<强> SQL FIDDLE DEMO

答案 1 :(得分:1)

如果您只是询问格式转换为上午9:30到上午10:00到09:00:00 - 10:00:00,那么您只需要将PRINT行更改为以下内容:

PRINT CONVERT(varchar,@StartTime,108)+ ' -- '+CONVERT(varchar,@InvTime,108)