Writing a Function in SQL to Loop Through a Date Range in a Udf

Writing a function in SQL to loop through a date range in a UDF

No need for functions:

select dhcp.singleday(a::date, a::date + 1)
from generate_series(
'2012-11-24'::date,
'2012-12-03',
'1 day'
) s(a)

This will work for any date range. Not only an inside month one.

Loop postgresql Function through Date Range

Place the function call in the FROM clause:

select f.*
from
generate_series('2015-01-01'::date,'2016-01-27','1 day') s(a),
myfun(a::date) f;

or using more formal syntax:

select f.*
from generate_series('2015-01-01'::date,'2016-01-27','1 day') s(a)
cross join myfun(a::date) f;

This form of the FROM clause is known as lateral join.

How to get dates between a date range given a day in SQL Server

I'll often use a TVF to create dynamic date ranges. You supply the desired range, DatePart, and increment. A Tally/Numbers Table or even a Calendar Table would do the trick as well

Example

Select *
From [dbo].[udf-Range-Date] ('2017-01-01','2017-02-06','DD',1)
Where DateName(DW,retval) = 'Monday'

Returns

RetSeq  RetVal
2 2017-01-02 00:00:00.000
9 2017-01-09 00:00:00.000
16 2017-01-16 00:00:00.000
23 2017-01-23 00:00:00.000
30 2017-01-30 00:00:00.000
37 2017-02-06 00:00:00.000

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

Hive UDF with date incrementing

You can do it using pure Hive query:

set hivevar:start_date=2019-01-02; 
set hivevar:end_date=2019-01-31;

with date_range as
(--this query generates date range
select date_add ('${hivevar:start_date}',s.i) as dt, s.i days
from ( select posexplode(split(space(datediff('${hivevar:end_date}','${hivevar:start_date}')),' ')) as (i,x) ) s
)

select dt, days from date_range;

Returns:

dt  days    
2019-01-02 0
2019-01-03 1
2019-01-04 2
2019-01-05 3
2019-01-06 4
2019-01-07 5
2019-01-08 6
2019-01-09 7
2019-01-10 8
2019-01-11 9
2019-01-12 10
2019-01-13 11
2019-01-14 12
2019-01-15 13
2019-01-16 14
2019-01-17 15
2019-01-18 16
2019-01-19 17
2019-01-20 18
2019-01-21 19
2019-01-22 20
2019-01-23 21
2019-01-24 22
2019-01-25 23
2019-01-26 24
2019-01-27 25
2019-01-28 26
2019-01-29 27
2019-01-30 28
2019-01-31 29

Hope you got the idea.

VBA UDF for list of MM.YYYY (months) between two dates

Something like the following gets the job done:

Option Explicit

Private Sub Test()
Debug.Print GetMonths(CDate("1/1/2021"), CDate("6/1/2021"))
End Sub

Private Function GetMonths(ByVal StartDate As Date, ByVal EndDate As Date) As String
Do While StartDate <= EndDate
GetMonths = GetMonths & Format(Month(StartDate), "00") & "." & Year(StartDate) & "; "
StartDate = DateAdd("m", 1, StartDate)
Loop

GetMonths = Left(GetMonths, Len(GetMonths) - 2)
End Function


Related Topics



Leave a reply



Submit