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 2008 Forums
 Transact-SQL (2008)
 Decimal to varchar or char

Author  Topic 

newsql1616
Starting Member

2 Posts

Posted - 2012-10-08 : 05:28:35
Hi,
I'm trying to convert a decimal (20,3) to a varchar or char length of 8. So for example if my decimal is 475.20 I need it to return 00000475 (without the pence), and then if it's 0.00 I need it to return 00000000.

Can somebody advise of the syntax I need? Tried searching but can't find anything

Thanks.

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2012-10-08 : 05:34:32
Try something like this:

DECLARE @n DECIMAL(20, 3) = 475.20;
SELECT RIGHT(REPLICATE('0', 8) + CAST(CAST(@n AS DECIMAL(8)) AS VARCHAR(8)), 8)




For us, there is only the trying. The rest is not our business. ~T.S. Eliot

Muhammad Al Pasha
Go to Top of Page

newsql1616
Starting Member

2 Posts

Posted - 2012-10-08 : 06:00:41
Spot on!

Thanks!
Go to Top of Page
   

- Advertisement -