Sunday, 30 June 2013

Generate list of months in sqlserver

DECLARE @year INT
SET @year = 2013

;WITH mths AS(
    SELECT 1 AS mth, DATENAME(MONTH, CAST(@year*100+1 AS VARCHAR) + '01')  AS monthname
    UNION ALL
    SELECT mth+1, DATENAME(MONTH, CAST(@year*100+(mth+1) AS VARCHAR) + '01') FROM mths WHERE mth < 12
)
SELECT * FROM mths 

No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...