T-SQL Generate months between dates from table - sql-server

I have table with month's ranges like this:
CREATE TABLE [dbo].[T_month_ranges](
[StartMonth] [datetime] NULL,
[EndMonth] [datetime] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[T_month_ranges] ([StartMonth],[EndMonth]) VALUES
('2015-02-01','2015-04-01')
INSERT INTO [dbo].[T_month_ranges] ([StartMonth],[EndMonth]) VALUES
('2016-12-01','2017-02-01')
INSERT INTO [dbo].[T_month_ranges] ([StartMonth],[EndMonth]) VALUES
('2017-08-01','2017-09-01');
These dates represents only year and month, first day is not important here.
Now we need to create select, that returns all months between dates in this table. So in the query result would be dates like these:
2015-02-01;2015-03-01;2015-04-01;2016-12-01;2017-01-01;2017-02-01;2017-08-01;2017-09-01
What is the best approach to achieve this in sql server ?

You can use a recursive CTE:
with m as (
select mr.startmonth as mon, mr.endmonth
from T_month_ranges mr
union all
select dateadd(month, 1, m.mon), m.endmonth
from m
where m.mon < m.endmonth
)
select m.mon
from m;
If your ranges are really wide (which seems unlikely with months), then you might need to set the MAXRECURSION option to 0.
An alternative -- which is probably faster -- is to use a table of numbers:
with n as (
select row_number() over (order by (select null)) - 1 as n
from master..spt_values
)
select dateadd(month, n.n, mr.startmonth)
from T_month_ranges mr join
n
on dateadd(month, n.n, mr.startmonth) <= mr.endmonth;

I'll often use a TVF to create dynamic date/time ranges. A tally/table would do the trick as well. The function is parameter driven, you supply the Range, DatePart, and Increment
For example:
Select A.*
,B.*
From T_month_ranges A
Cross Apply [dbo].[udf-Range-Date](A.StartMonth,A.EndMonth,'MM',1) B
Returns
The UDF if interested.
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)
*/
Option 2 (without a UDF)
Select A.*
,B.*
From T_month_ranges A
Cross Apply (
Select Top (DateDiff(MM,A.Startmonth,A.EndMonth)+1)
Date=DateAdd(MM,Row_Number() over (Order by (Select null)) - 1,A.Startmonth)
From master..spt_values
) B

Related

How do I can divide a day in 30 minutes intervals in SQL server for a single date or multiple days like a month [duplicate]

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.
For example:
#DateTime1 = 24/11/2016 18:00:00
#DateTime2 = 25/11/2016 06:00:00
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
What I've tried.
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
A tally table is a great way to deal with this type of thing. I keep one in a view to avoid using 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
Then your code becomes really simple too. A small amount of datemath and voila.
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
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)
Returns
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
The UDF if needed
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)
*/
using a recursive Common Table Expression [CTE] is one pretty clean method. For the formatting I am showing FORMAT() from SQL-Server 2012+ you may consider using DATEPART etc to do it though as FORMAT() can have performance impact.
I do agree with #RossBush's comment if you do things like this a lot generating a calendar (dates) and a time dimensions is very helpful for these purposes.
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
Please see if this works.
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
What about this? You can use variables/ fixed values as necessary.
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

SQL Server Function - CTE defined but not used

I am trying to create function in SQL Server that returns months between range of dates passed to function. Below is my current script -
CREATE FUNCTION [dbo].[fn_get_date_range] (
#startDate datetime,
#endDate datetime,
#frequency varchar(20)
)
RETURNS
#dateRanges TABLE
(
report_date datetime
)
AS
BEGIN
WITH DateRange (report_date) AS
(
SELECT row_number() OVER (ORDER BY object_id) FROM sys.all_objects)
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#StartDate)+report_date,0)
) AS report_date
insert into #dateRanges (report_date)
Select report_date FROM DateRange
WHERE report_date <= DATEDIFF(month, #StartDate, #EndDate)+1
RETURN
END
But when I try to create these function, I get below error -
Common table expression defined but not used.
I have tried different approach but it doesn't work. Any pointers in right direction would be appreciated. Thank you.
Here is my TVF used for dynamic datetime ranges.
Similar to your approach, but I do have the DATEPART as a parameter as well as the increment.
Example - Month Increment 1:
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1)
Returns
Example - Minute Increment 15:
Select * from [dbo].[tvf-Range-Date]('2017-01-01','2017-01-02','MI',15)
Returns
The UDF if Interested
CREATE FUNCTION [dbo].[tvf-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].[tvf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/
EDIT - As Requested
CREATE FUNCTION [dbo].[tvf-Range-Date-Span] (#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
,RetVal1 = D
,RetVal2 = LEAD(D,1,#R2) over (Order By D)
From cte3,cte0
Where N<cte0.M-1
)
--Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
--Select * from [dbo].[tvf-Range-Date-Span]('2016-10-01','2020-10-01','YY',1)
Returns something like this
Edit 2 - For 2008
;with cte as (
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1)
)
Select DateR1 = A.RetVal
,DateR2 = B.NxtDate
From cte A
Cross Apply (Select NxtDate=min(RetVal) from cte where RetVal > A.RetVal ) B
Where B.NxtDate is not null
Returns
It is not allowed that CTE is not followed by SELECT statement, cite from MS:
•A CTE must be followed by a single SELECT statement. INSERT, UPDATE,
DELETE, and MERGE statements are not supported.
UPDATE
CREATE FUNCTION [dbo].[fn_get_date_range] (
#startDate datetime,
#endDate datetime,
#frequency varchar(20)
)
RETURNS
#dateRanges TABLE
(
report_date datetime
)
AS
BEGIN
WITH DateRange (report_date) AS
(
SELECT row_number() OVER (ORDER BY object_id) FROM sys.all_objects)
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#StartDate)+report_date,0)
) AS report_date
Select * FROM DateRange
INTO #temp_table
WHERE report_date <= DATEDIFF(month, #StartDate, #EndDate)+1
insert into #dateRanges (report_date)
select * from #temp_table
drop table #temp_table
RETURN
END
This is a variant that's similar to John's function...
CREATE FUNCTION dbo.tfn_BuildDateRanges
(
#startDate DATETIME,
#endDate DATETIME,
#dateInterval VARCHAR(20),
#interval INT
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
WITH
cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),
cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),
cte_n3 (n) AS (SELECT 1 FROM cte_n2 a CROSS JOIN cte_n2 b),
cte_Calendar AS (
SELECT TOP ((CASE
WHEN #dateInterval IN ('year', 'yy', 'yyyy') THEN DATEDIFF(year, #startDate, #endDate)
WHEN #dateInterval IN ('quarter', 'qq', 'q') THEN DATEDIFF(quarter, #startDate, #endDate)
WHEN #dateInterval IN ('month', 'mm', 'm') THEN DATEDIFF(month, #startDate, #endDate)
WHEN #dateInterval IN ('week wk, ww') THEN DATEDIFF(week, #startDate, #endDate)
WHEN #dateInterval IN ('day', 'dd', 'd') THEN DATEDIFF(day, #startDate, #endDate)
WHEN #dateInterval IN ('hour', 'hh') THEN DATEDIFF(hour, #startDate, #endDate)
WHEN #dateInterval IN ('minute', 'mi', 'n') THEN DATEDIFF(minute, #startDate, #endDate)
WHEN #dateInterval IN ('second', 'ss', 's') THEN DATEDIFF(second, #startDate, #endDate)
END / #interval + 1))
BegOfRange = CASE
WHEN #dateInterval IN ('year', 'yy', 'yyyy') THEN DATEADD(year, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('quarter', 'qq', 'q') THEN DATEADD(quarter, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('month', 'mm', 'm') THEN DATEADD(month, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('week wk, ww') THEN DATEADD(week, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('day', 'dd', 'd') THEN DATEADD(day, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('hour', 'hh') THEN DATEADD(hour, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('minute', 'mi', 'n') THEN DATEADD(minute, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
WHEN #dateInterval IN ('second', 'ss', 's') THEN DATEADD(second, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * #interval, #startDate)
END,
EndOfRange = CASE
WHEN #dateInterval IN ('year', 'yy', 'yyyy') THEN DATEADD(year, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('quarter', 'qq', 'q') THEN DATEADD(quarter, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('month', 'mm', 'm') THEN DATEADD(month, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('week wk, ww') THEN DATEADD(week, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('day', 'dd', 'd') THEN DATEADD(day, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('hour', 'hh') THEN DATEADD(hour, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('minute', 'mi', 'n') THEN DATEADD(minute, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
WHEN #dateInterval IN ('second', 'ss', 's') THEN DATEADD(second, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * #interval, #startDate)
END
FROM
cte_n3 a CROSS JOIN cte_n3 b
)
SELECT
c.BegOfRange,
c.EndOfRange
FROM
cte_Calendar c;
GO

MSSQL: Generate Dates Between 2 Dates With Month Breaks

Struggling a bit with this...
I have a table with the following columns:
Type varchar
StartDate datetime
EndDate datetime
Interval int
What I want is a results table with the type column and all the dates between the start date and end date, using the interval as month breaks between them.
For instance if the only row in my table is:
'Test', '2017-01-01', '2019-01-01', 6
I want a results table that has 5 rows, with 'Test' as the Type on every row, and a date column that goes from:
'2017-01-01',
'2017-07-01',
'2018-01-01',
etc.
I've created a Calendar table and even a DateRange function where I pass in a start date, end date and month interval which returns me all the dates I'd want, but since it's a table-valued function I don't seem to be able to call it with data from another table.
How do I do this?
Thanks.
I'll often use a TVF to create dynamic Date/Time Ranges. A tally/calendar table will do the trick as well, but the UDF offers some additional functionality. For example, you supply the Range, DatePart and Increment
Declare #YourTable table ([Type] varchar(25),StartDate date,EndDate date,Interval int)
Insert Into #YourTable values
('Test','2017-01-01','2019-01-01', 6)
Select A.[Type]
,B.*
From #YourTable A
Cross Apply [dbo].[udf-Range-Date](A.StartDate,A.EndDate,'MM', A.Interval) B
Returns
Type RetSeq RetVal
Test 1 2017-01-01
Test 2 2017-07-01
Test 3 2018-01-01
Test 4 2018-07-01
Test 5 2019-01-01
The UDF if Interested
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)
*/

How can I get first day of from YYYYWW in tsql

Can someone help me with getting the first and last day of week based on yearweek integer like 201648 without conserning about setting the ##firstdate attribute. I want iso date starting on monday in datetime format.
declare #yrwk int = 201648
declare #yr int = left(#yrwk,4)
declare #wk int = right(#yrwk,2)
select dateadd (week, #wk, dateadd (year, #yr-1900, 0)) - 4 - datepart(dw, dateadd (week, #wk, dateadd (year, #yr-1900, 0)) - 4) + 1
--returns 11/27/2016 which is Sunday of that week (start of week)
--change +1 to +2 at the end for "Monday"
After a little consideration, I thought that perhaps my dynamic Date/Time Range UDF may help here. I use this UDF to generate dynamic date/time ranges. You can supply the desired date range, date part and increment. A tally table would do the trick as well
In this case, we are getting the Nth Monday regardless of the datepart(WK,..) as per the requirements.
Declare #YYYYWW int = 201648
Select WkNbr = B.RetSeq
,WkBeg = B.RetVal
,WkEnd = DateAdd(DD,6,B.RetVal)
From (
Select MinDate=Min(RetVal)
From [dbo].[udf-Range-Date](DateFromParts(Left(#YYYYWW,4),1,1),DateFromParts(Left(#YYYYWW,4),1,10),'DD',1)
Where DateName(DW,RetVal)='Monday'
) A
Cross Apply (Select * From [dbo].[udf-Range-Date](A.MinDate,DateFromParts(Left(#YYYYWW,4),12,31),'DD',7) ) B
Where B.RetSeq = Right(#YYYYWW,2)
Returns
WkNbr WkBeg WkEnd
48 2016-11-28 2016-12-04
The UDF if interested
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 minute time intervals beteen 2 datetime

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.
For example:
#DateTime1 = 24/11/2016 18:00:00
#DateTime2 = 25/11/2016 06:00:00
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
What I've tried.
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
A tally table is a great way to deal with this type of thing. I keep one in a view to avoid using 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
Then your code becomes really simple too. A small amount of datemath and voila.
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
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)
Returns
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
The UDF if needed
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)
*/
using a recursive Common Table Expression [CTE] is one pretty clean method. For the formatting I am showing FORMAT() from SQL-Server 2012+ you may consider using DATEPART etc to do it though as FORMAT() can have performance impact.
I do agree with #RossBush's comment if you do things like this a lot generating a calendar (dates) and a time dimensions is very helpful for these purposes.
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
Please see if this works.
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
What about this? You can use variables/ fixed values as necessary.
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

Resources