Get a table with (months + year) between two dates using MSSQL

DECLARE @start DATE = GETDATE()
DECLARE @nth INT = 3
DECLARE @end DATE = DATEADD(MONTH, @nth, GETDATE());

WITH cte
AS (
SELECT dt = DATEADD(DAY, – (DAY(@start) – 1), @start)

UNION ALL

SELECT DATEADD(MONTH, 1, dt)
FROM cte
WHERE dt < DATEADD(DAY, – (DAY(@end) – 1), @end)
)
SELECT CONVERT(CHAR(4), dt, 100) + CONVERT(CHAR(4), dt, 120)
FROM cte

Happy Coding 🙂

Advertisements

2 thoughts on “Get a table with (months + year) between two dates using MSSQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s