Avatar of Zolf
Zolf
Flag for United Arab Emirates asked on

Add leading zero when number is less than 10?


Hi there,

My database is MySQL and I have a sql query which returns the month and I want to add 0 before the single digit month.

select distinct monthname,onthNumberOfYearfrom datedimension p 
where datekey >= 13990101

Open in new window

DatabasesSQLMySQL Server

Avatar of undefined
Last Comment
slightwv (䄆 Netminder)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
slightwv (䄆 Netminder)

Yes, you could do it with two function calls.  I can probably come up with a way to use even more than two.

Normally you do it with one:  LPAD.

https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad

lpad(MonthNumberOfYearfrom ,2,'0')
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23