Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have an month Id column in my table and inserting values like 1,2,3 etc.

and want to select an month name instead of integer values.

I am trying the following syntax,

datename(month,MonthId)as Month but in all the coloumn it shows january only.

please tell me how to get the result.
Posted

The dataname function expects a date as second parameter, try this trick:
SQL
SELECT DATENAME(MONTH, DATEADD(MONTH, monthid - 1, CAST('2014-01-01' AS DATETIME)))
FROM table1
 
Share this answer
 
v3
That's because DATENAME doesn't accept a month number - it takes a DATE value. So when you provide it with a number between 1 and 12, it assumes that is the number of days into the year...
You could "fiddle" it by ensuring the month is a zero based value and multiplying it by 30, but it's a kludge and I don't like it much...
 
Share this answer
 
you can also do it from code behind

as below
using System.Globalization

C#
DateTimeFormatInfo m = new DateTimeFormatInfo();
                string Month=m.GetMonthName(int Month_No)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900