mssql 30分钟时间间隔beteen 2 datetime

时间:2016-11-03 17:49:06

标签: sql-server date datetime

我有以下查询,我希望在2个日期时间之间以30分钟的间隔获取日期时间。基本上我得到了它,但如果timediff超过24小时,则会被限制并且不会返回结果。

例如:

@DateTime1 = 24/11/2016 18:00:00
@DateTime2 = 25/11/2016 06:00:00

结果:(格式为“dd-HH:mm”)

24-18:00
24-18:30
24-19:00
24-19:30
24-20:00
...
...
25-05:00
25-05:30
25-06:00

我尝试了什么。

SELECT        number, DATEADD(MINUTE, number, @DateTime1) AS DateTimeLine, DATEPART(DAY, DATEADD(MINUTE, number, @DateTime1)) AS Days, DATEPART(MONTH, 
DATEADD(MINUTE, number, @DateTime1)) AS Months, DATEPART(YEAR, DATEADD(MINUTE, number, @DateTime1)) AS Years, DATEPART(HOUR, DATEADD(MINUTE,
number, @DateTime1)) AS Hours, DATEPART(MINUTE, DATEADD(MINUTE, number, @DateTime1)) AS Minute, CAST(DATEADD(MINUTE, number, @DateTime1) 
AS DATE) AS Date, CAST(DATEADD(MINUTE, number, @DateTime1) AS TIME) AS Time

FROM         master.dbo.spt_values

WHERE        (type = 'P') AND (DATEPART(MINUTE, DATEADD(MINUTE, number, @DateTime1)) = 30 OR DATEPART(MINUTE, DATEADD(MINUTE, number, @DateTime1)) = 0) AND (DATEADD(MINUTE, number, @DateTime1) <= @DateTime2)

ORDER BY number

5 个答案:

答案 0 :(得分:1)

计数表是处理此类事物的好方法。我保留一个以避免使用spt_values。

create View [dbo].[cteTally] as

WITH
    E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
    E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
    cteTally(N) AS 
    (
        SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
    )
select N from cteTally

然后你的代码变得非常简单。少量的日期和瞧。

declare @DateTime1 datetime = '2016/11/24 18:00:00'
    , @DateTime2 datetime = '2016/11/25 06:00:00'

select FORMAT(DATEADD(minute, (t.N - 1) * 30, @DateTime1), 'dd-HH:mm')
from cteTally t
where t.N <= (DATEDIFF(hour, @DateTime1, @DateTime2) * 2) + 1

答案 1 :(得分:0)

使用递归公用表表达式[CTE]是一种非常干净的方法。对于我在SQL-Server 2012+中显示FORMAT()的格式,您可以考虑使用DATEPART等来执行此操作,但FORMAT()会对性能产生影响。

我同意@ RossBush的评论,如果您做这样的事情会产生很多日历(日期),时间维度对于这些目的非常有帮助。

DECLARE @DateTime1 DATETIME = '2016/11/24 18:00:00'
DECLARE @DateTime2 DATETIME = '2016/11/25 06:00:00'

;WITH cte30MinIncrements AS (
    SELECT @DateTime1 as DT

    UNION ALL

    SELECT DATEADD(MINUTE,30,DT)
    FROM
       cte30MinIncrements
    WHERE DATEADD(MINUTE,30,DT) <= @DateTime2
)

SELECT
    *
    ,FORMAT(DT,'dd-HH:mm') as Formated
FROM
    cte30MinIncrements

答案 2 :(得分:0)

请查看是否有效。

declare @DateTime1 DateTime = '2016-11-24 18:00:00'
declare @DateTime2 DateTime = '2016-11-25 18:00:00'

declare @Interval DateTime = @DateTime1
declare @vartmptable table(DT DateTime)

While (@Interval < @DateTime2)
begin
 --select @Interval, FORMAT(@Interval,'dd-HH:mm')
 insert into @vartmptable select @Interval
 set @Interval = DATEADD(mi,30,@Interval)

end

select FORMAT(DT,'dd-HH:mm')  from @vartmptable

答案 3 :(得分:0)

我有一个生成动态日期/时间范围的TVF。它比递归cte更快,我认为更灵活。您传递日期范围,所需的DatePart和增量。

Declare @DateTime1 DateTime = '2016-11-24 18:00:00'
Declare @DateTime2 DateTime = '2016-11-25 06:00:00'

Select Format(RetVal,'dd-HH:mm') from [dbo].[udf-Range-Date](@DateTime1,@DateTime2,'MI',30)

返回

24-18:00
24-18:30
24-19:00
24-19:30
24-20:00
24-20:30
24-21:00
24-21:30
24-22:00
24-22:30
24-23:00
24-23:30
25-00:00
....
25-04:30
25-05:00
25-05:30
25-06:00

UDF(如果需要)

CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
    with cte0(M)   As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
         cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
         cte2(N)   As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
         cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )

    Select RetSeq = N+1
          ,RetVal = D 
     From  cte3,cte0 
     Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1) 
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1) 
*/

答案 4 :(得分:0)

这个怎么样?您可以根据需要使用变量/固定值。

WITH CTE_Numbers
AS (
    SELECT n = 1

    UNION ALL

    SELECT n + 1
    FROM CTE_Numbers
    WHERE n < 100
    )
SELECT FORMAT(DATEADD(mi, n * 30, '2016/11/03'),'dd-HH:mm')
FROM CTE_Numbers