February 8, 2008 at 12:23 am
Hi all
Is there any best way to get month name in SQL SERVER 2005.
I know that we can do this using our own function, but just i want to conform that if SQL SERVER provide any function for month name if we have month number i.e. 1,2..12
Thanks
Warm Regards,
Shakti Singh Dulawat
Before printing, think about the environment
Do the impossible, and go home early.
February 8, 2008 at 1:30 am
SQL has a DateName function, however it takes a datetime as a parameter, not a number
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
returns
Month Name
------------------------------
February
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 8, 2008 at 1:45 am
Thanks
Thats good
But is there is way so we can get month name using month number.
Warm Regards,
Shakti Singh Dulawat
Before printing, think about the environment
Do the impossible, and go home early.
February 8, 2008 at 1:52 am
Not directly, though you could convert the month number inot a date.
SELECT DATENAME(month,'1900/' + CAST(monthNumber AS VARCHAR(2)) + '/01')
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 8, 2008 at 10:36 pm
Thanks very much
Warm Regards,
Shakti Singh Dulawat
Before printing, think about the environment
Do the impossible, and go home early.
March 11, 2008 at 8:50 am
thank you GilaMonster...
March 11, 2008 at 9:15 am
GilaMonster (2/8/2008)
Not directly, though you could convert the month number inot a date.SELECT DATENAME(month,'1900/' + CAST(monthNumber AS VARCHAR(2)) + '/01')
Heh, too much VARCHAR in that for me, Gail... 😛
SELECT DATENAME(mm,DATEADD(mm, MonthNumber-1,0))
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2009 at 6:50 am
thanks a ton.....
it helped. 😀
March 11, 2009 at 9:33 pm
You bet... thanks for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 12, 2009 at 3:30 am
A little modification in Jeff's code.
SELECT DateName(mm,DATEADD(mm,MonthNumber,-1))
karthik
March 12, 2009 at 6:55 pm
karthikeyan (3/12/2009)
A little modification in Jeff's code.SELECT DateName(mm,DATEADD(mm,MonthNumber,-1))
Yep... good call.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 18, 2010 at 2:36 am
Super Logic..Keep Rocking:)
March 29, 2010 at 2:43 am
Here is one line solution in my implementation.
http://praveenbattula.blogspot.com/2009/04/how-to-know-month-name-from-month-index.html
April 2, 2010 at 6:11 pm
The character conversion will slow things down quite a bit if you happen to use the for batch programming on millions of rows.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 7, 2010 at 10:49 pm
[Edit] Ack, thought I had scrolled down all the way, apparently I was wrong.
Since I've posted anyways, might as well mention that doing it this way automatically adjusts for language settings.
set language spanish
select DATENAME(m,DATEADD(m,1,-1))--Enero
select DATENAME(m,DATEADD(m,2,-1))--Febrero
select DATENAME(m,DATEADD(m,3,-1))--Marzo
select DATENAME(m,DATEADD(m,4,-1))--Abril
Viewing 15 posts - 1 through 15 (of 29 total)
You must be logged in to reply to this topic. Login to reply