mssql 30分钟的时间间隔beteen 2 datetime [英] mssql 30 minute time intervals beteen 2 datetime

查看:136
本文介绍了mssql 30分钟的时间间隔beteen 2 datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询,我想在2 datetime之间的30分钟间隔中获取datetime。基本上我知道了,但是受到限制,如果timediff超过24小时,则不会返回任何结果。

I have below query and i want to get datetime in 30 min intervals between 2 datetime. Basicly I got it, but is limitited and wouln't return al results if the timediff is over 24 hrs.

例如:

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

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

Result: (in format "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


推荐答案

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

I have a TVF which generates dynamic date/time ranges. It is faster than a recursive cte, and I think more flexible. You pass the date range, desired DatePart, and increment.

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) 
*/

这篇关于mssql 30分钟的时间间隔beteen 2 datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆