Vincent Ducroquet
Vincent Ducroquet

Reputation: 924

How to get occurence every n weeks in SQL/TSQL

I have a function names CronTabSchedule in a DLL to generate date occurence from a CRON expression. I added a column which returns the week number of each date

Here is the call of the function:

SELECT DATEPART( wk, Occurrence) as week_number, Occurrence FROM dbo.CrontabSchedule('0 18 * * 2', '2017-3-7', '2017-3-31')

So my CRON expression is 'every tuesday at 18:00'

It return a table like that :

Occurence    week_number
2017/03/07        8
2017/03/14        9
2017/03/21       10
2017/03/28       11

My problem is CRON expressions can't handle 'every n weeks'

So i'd like to, in SQL or T-SQL, get all occurence(s) of the table every n week

Ex: Every 2 weeks will return :

 Occurence    week_number
2017/03/07        8
2017/03/21       10

Ex2: Every 3 weeks will return :

 Occurence    week_number
2017/03/07        8
2017/03/28       11

Upvotes: 1

Views: 708

Answers (3)

S3S
S3S

Reputation: 25142

I think your function has an error since the week numbers aren't correct. But if they were, this would work:

declare @table table (dates datetime, week_number int)
insert into @table values
('2017/03/07',10),
('2017/03/14',11),
('2017/03/21',12),
('2017/03/28',13)



declare @NumberOfWeeks int = 3


select 
    *
from @table 
where
    week_number%@NumberOfWeeks =  datepart(week,'2017-3-7')%@NumberOfWeeks
--just be sure to set the start date of your cron job in this where clause
--or you could make it a parameter.

Upvotes: 0

jfatal
jfatal

Reputation: 253

Here's a simple solution:

(Every 2 weeks)

Select * From Table
where week_number %2 = 1

(Every 3 weeks)

Select * From Table
where week_number %3 = 1

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 82000

I often use a Table-Valued-Function to create dynamic date/time ranges. Faster than a recursive cte (especially for larger ranges) and offers a little more functionality: User Supplied Date/Time Range, DatePart and Increment.

Also easy to incorporate into a CROSS APPLY

Example 1

Select * from [dbo].[udf-Range-Date]('2017-03-07','2017-04-01','WK',1)

Returns

RetSeq  RetVal
1       2017-03-07 00:00:00.000
2       2017-03-14 00:00:00.000
3       2017-03-21 00:00:00.000
4       2017-03-28 00:00:00.000

Example 2

Select * from [dbo].[udf-Range-Date]('2017-03-07','2017-04-01','WK',2)

Returns

RetSeq  RetVal
1       2017-03-07 00:00:00.000
2       2017-03-21 00:00:00.000

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

Upvotes: 1

Related Questions