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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Decimal to varchar or char
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

newsql1616
Starting Member

2 Posts

Posted - 10/08/2012 :  05:28:35  Show Profile  Reply with Quote
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

Saudi Arabia
264 Posts

Posted - 10/08/2012 :  05:34:32  Show Profile  Reply with Quote
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 - 10/08/2012 :  06:00:41  Show Profile  Reply with Quote
Spot on!

Thanks!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.2 seconds. Powered By: Snitz Forums 2000