From a set of data in SQL Server database, we needed to find a way to quickly get a numeric representation (add a field MonthNo) of a month name in order to do a comparison. Given we only have a month name and year fields here’s a simple way to do it.
Original SQL:
SELECT [Id], [Month], [Year] FROM Table1
Add a field called MonthNo:
MONTH(CONVERT(NVARCHAR, [Month]) + ' ' + CONVERT(NVARCHAR, [Year]) ) AS MonthNo
Final SQL with the new column:
SELECT [Id] , MONTH(CONVERT(NVARCHAR, [Month]) + ' ' + CONVERT(NVARCHAR, [Year])) AS [MonthNo] , [Month], [Year] FROM Table1