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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 leading zero

Author  Topic 

jtwork
Yak Posting Veteran

82 Posts

Posted - 2008-05-16 : 04:07:08

declare @Mymonth as varchar(2)

set @MyMonth = '0' + datepart(m,getdate())

print @MyMonth



why wont the above produce 05 just 5

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2008-05-16 : 04:16:44
because datepart returns int...convert it to varchar first...

declare @Mymonth as varchar(2)
set @MyMonth = '0' + convert(varchar(2),datepart(m,getdate()))
print @MyMonth

--------------------
keeping it simple...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 11:04:33
quote:
Originally posted by jen

because datepart returns int...convert it to varchar first...

declare @Mymonth as varchar(2)
set @MyMonth = RIGHT('0' + convert(varchar(2),datepart(m,getdate())),2)
print @MyMonth

--------------------
keeping it simple...


or this if you always want month in two digits
i.e 10,11,12 instead of 010,011,...
Go to Top of Page
   

- Advertisement -