Archive | January, 2012

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