Monday, December 12, 2011

How to List Of Month Year Between Date Range ?

How to show or list Month with Year Between Date Range in SQL

CREATE TABLE #temp_report(id INT IDENTITY(1,1), dates DATE,display_date VARCHAR(20))

DECLARE @i INT
DECLARE @count INT
DECLARE @from_date date,
DECLARE @to_date date

SELECT
@from_date = '12/8/2000'
SELECT @to_date = '12/8/2012'
SELECT @count = DATEDIFF(mm, @from_date,@to_date )
SELECT @i =0

WHILE(@i<=@count)
BEGIN
INSERT INTO #temp_report(dates,display_date)
SELECT DATEADD(MM,@i,@from_date),CONVERT(VARCHAR(3),DATEADD(MM,@i,@from_date),109)+''''+ SUBSTRING(CONVERT(VARCHAR(10),DATEADD(MM,@i,@from_date),101),9,2)
SELECT @i = @i + 1
END

SELECT * FROM #te
mp_report

No comments:

Post a Comment