Friday, June 24, 2016

MonthName and Month Number List between Two Date in SQL Server

//Month Name and Month Number List between Two Date in SQL Server

SELECT  MONTH(DATEADD(MONTH, x.number, '2013-05-31'))[MonthNo],
DATENAME(MONTH, DATEADD(MONTH, x.number,  '2013-05-31'))[Month],
DATENAME(YEAR, DATEADD(MONTH, x.number,  '2013-05-31'))[Year] ,
LEFT(DATENAME(MONTH, DATEADD(MONTH, x.number,  '2013-05-31')),3) + ' ' + DATENAME(YEAR, DATEADD(MONTH, x.number,  '2013-05-31')) [Name]
FROM    master.dbo.spt_values x
WHERE   x.type = 'P'      
AND     x.number <= DATEDIFF(MONTH,  '2013-05-31',  '2014-05-31')


No comments: