Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Format Part of GETDATE()

Author  Topic 

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2013-12-13 : 05:11:56
I'm trying to return the month part of the current date, along with the month number. I want it to return:

01 - January
02 - February, etc

It's fine for October to December, but for the other nine months my code returns 1 - January and I want to be sure the leading 0 is added. How can I do this? Here is my current code:

CONVERT(nvarchar,MONTH(Getdate())) + ' - ' + DATENAME(MONTH,Getdate()) as MonthName


Many thanks
Martyn


webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-12-13 : 05:16:51
right('00'+CONVERT(nvarchar,MONTH(Getdate())),2) + ' - ' + DATENAME(MONTH,Getdate()) as MonthName


Too old to Rock'n'Roll too young to die.
Go to Top of Page

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2013-12-13 : 05:36:00
Works perfectly, thank you!

Martyn
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-13 : 06:45:03
REPLACE(STR(MONTH(Getdate()),2),' ','0')+ ' - ' + DATENAME(MONTH,Getdate()) as MonthName


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-12-24 : 07:37:16
In version 2012 onwards,

SELECT format(getdate(),'MM-MMMM')

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -