top of page
Search
Writer's pictureVictor Escott

SQL-How to generate a sequence of dates between two given dates?

I recently received a very interesting question, one of my connections in LinkedIn asked if I know how to generate a sequence of dates between two given dates?


The first thing that came to my mind was to create a calendar table since they are very useful, he told me that that sounds good, but he really wants to know how to write the query.


This is what he was looking for:



Now let us explore three different methods which will generate the sequence.


Method 1: Using WHILE loop

There is usually a set based alternative that will perform much better.

Method 2: Using Recursive CTE

Method 3: Using Cross Join and Top

The above methods produce the following result:


Let me know if you know any other methods in the comments section :).


Enjoy!!

69 views0 comments

Recent Posts

See All

Comments


bottom of page