Skip to content
Advertisement

SQL Server – Retrieve list of month dates between two columns

I want to add a new column that should contain months between startdate & enddate present in two separate columns.

My current data looks something like this :

Case Name StartDate EndDate
1 ABC 2021-01-15 2021-03-15
2 DEF 2021-03-15 2021-05-15

My desired output is :

Case Name StartDate EndDate MonthList
1 ABC 2021-01-01 2021-03-15 2021-01-15
1 ABC 2021-01-01 2021-03-15 2021-02-15
1 ABC 2021-01-01 2021-03-15 2021-03-15
2 DEF 2021-03-01 2021-05-15 2021-03-15
2 DEF 2021-03-01 2021-05-15 2021-04-15
2 DEF 2021-03-01 2021-05-15 2021-05-15

Advertisement

Answer

see (Generate Dates between date ranges)
use a cross join to combine your tmp table with the date generator code

output:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement