September 20, 2006 at 2:18 pm
Hi guys ! i have a column in my table which has got month numbers. I want to convert these numbers to month names.How do i do that?
Cheers
Mita
September 20, 2006 at 2:34 pm
Add the month number to an arbitrary date and use the DATENAME function.
DECLARE @date datetime
DECLARE @MonthColumns table (MonthNumber int)
insert into @MonthColumns
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 12
SET @Date = '01/01/01'
SELECT DATENAME(mm,DATEADD(mm,(MonthNumber - 1),@date))
FROM @MonthColumns
September 20, 2006 at 3:33 pm
If you want to use it against the column in the table try this:
select DATENAME(mm,ColumnName)
Thanks
Sreejith
September 20, 2006 at 3:37 pm
"If you want to use it against the column in the table try this:select DATENAME(mm,ColumnName)"
This will not work as the original post says that the column only holds the month number and not the full date. This is why I suggested using DATEADD with an arbitrary date.
September 20, 2006 at 3:46 pm
ya because my column just has month numbers .there is no date in it.
I just tried the query which u suggeste. It gave me the names of all the months.
So now what I have to do?
Sorry for my lack of knowledge.I am still learning
September 20, 2006 at 3:50 pm
Substitute your table name for the @MonthColumns table variable and your column name for MonthNumber. For example:
DECLARE @date datetime
SET @Date = '01/01/01'
SELECT DATENAME(mm,DATEADD(mm,(<YOUR COLUMN> - 1),@date))
FROM <YOUR TABLE>
September 20, 2006 at 4:06 pm
Cheers buddy
It works!!!
Thanks for help
September 21, 2006 at 6:53 am
The posted solutions seem overly complicated to me. I'd do something like this:
select datepart(mm, convert(datetime, convert(varchar(2), MonthNumberColumn) + '/20/2006')) from MonthNumberTable
The day and year in the string ('/20/2006') don't really matter, just make the day number > 12 so the resulting date is unambiguous.
Good luck
September 21, 2006 at 7:48 am
or
DATENAME(month,DATEADD(month,[monthcolumn],-1))
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy