FOR GETTING MONTHS BETWEEN TWO DATES IN SQL

20 Jan

For getting month names between two dates we may use the following Query

DECLARE @date1 DATETIME,@date2 DATETIME

SET @date1 = ‘2010-12-20’

SET @date2 = GETDATE();

WITH cte

AS (

SELECTdatename(month, @date1) AS [Month_Name], @date1 AS dat

UNIONALL

SELECTdatename(month, DateAdd(Month, 1, dat)), DateAdd(Month, 1, dat) FROM cte

WHEREDateAdd(Month, 1, dat) < @date2

)

SELECT [Month_Name]

FROM CTE

 

OURTPUT

Advertisements

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

%d bloggers like this: