1

I'm trying to find all the saturdays between two dates (inclusive) without using a loop.

For example, April 1, 2021, to May 1, 2021 should return:

04-03-2021
04-10-2021
04-17-2021
04-24-2021
05-01-2021
userone
  • 173
  • 4
  • 14

1 Answers1

1

You can use datename

    select *
    from Table
    where Datename(dw,Datecolumn)='Saturday'
    and DateColumn >= start and Datecolumn < End;

Alternatively, if you just want to generate a list on the fly, you can do

declare @StartDate date = '20210401', @EndDate date = '20210501';

select DateAdd(day, rn-1, @StartDate)
from ( 
    select Row_Number() over (order by object_id) rn
    from sys.columns
 ) d
where rn - 1 <= DateDiff(day, @StartDate, @EndDate)
and DateName(dw, DateAdd(day, rn-1, @StartDate))='Saturday';

This first generates a list of numbers in the inner query by utilising one of several possible system tables to select just a row number. This is essentially building a numbers / tally table on the fly, and it's usually a good idea to have a permanent table like this available (a single column of integers starting at 1 and going up to a squintillion or whatever you need). You can see how this works by just highlighting the inner query and running it (F5 in SSMS).

An out query selects from this and filters the row numbers to just the right sequential range for the number of days between the two specified dates. Again, you can check this by highlighting the entire query except for the last line, you'll see it generates the list of dates between the specified start and end dates inclusively.

Finally the and criteria extracts the name of the day for each date and further filters the list of dates based on the day's name.

Stu
  • 30,392
  • 6
  • 14
  • 33
  • Oh, I didn't see your edit before posting my answer. I'll delete mine. – Pierre-Alain Vigeant Apr 25 '21 at 20:17
  • @Pierre-AlainVigeant Lolz - I thought it best to offer both as it's not really clear what the intended use-case is. You're right on the use of a numbers table being useful, I have one in every database for many similar uses. – Stu Apr 25 '21 at 20:19
  • Hi, thanks for the quick reply! I tested this and it works but... could you please explain what it does? I can understand parts of it but not in its entirety – userone Apr 25 '21 at 20:36
  • @userone Which part - first query or second - is the issue and what specifically? – Stu Apr 25 '21 at 20:38
  • the 2nd query and quite frankly, other than the last line (which i assume is filtering by saturday) i can only make guesses – userone Apr 25 '21 at 20:40
  • @userone No problem, I've added a description above, hopefully that helps you follow it! – Stu Apr 25 '21 at 20:49
  • @stu yep it took a while but your comment and a few more runs helped me understand it! Basically it's creating X number of rows (number of days from start to end) and adding each day (1, 2, 3, etc.) from the start date. Lastly it just filters those dates based on specified weekday. Question though, what if there isn't enough columns in sys.columns? IE. I choose two dates thats like a thousand days apart and i have a brand new database? – userone Apr 25 '21 at 20:56
  • Great, glad you got it! If you found it useful, by all means mark it as accepted :-) – Stu Apr 25 '21 at 21:03