Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get datepart month with 2 numbers

Status
Not open for further replies.
Jun 27, 2001
837
US
I understand how datepart works. However, if I am asking for the month datepart(month,getdate()) is there a way to force the months to be 2 digits (say for January be 01 instead of 1)
 
Datepart returns an integer value. It won't have a leading zero. You can always convert the integer to a character string and add the leading zero. Here is one method.

replace(str(month(getdate()), 2), ' ', '0')

month(getdate()) is equivalent to datepart(month,getdate()) Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Datepart returns integers '01' will always be 1.

SELECT CASE
WHEN datePart(mm,getDate())< 10 THEN '0' + cast(datepart(mm,getDate()) as char(1))
ELSE cast(datepart(mm,getDate()) as char(2))
END -----------------------------------------------------------------
[pc] Be nice. It's only doing what you tell it to do.
mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top