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
 General SQL Server Forums
 New to SQL Server Programming
 Formating Number with leading zeros keeping signs

Author  Topic 

sonetav
Starting Member

3 Posts

Posted - 2010-07-27 : 13:21:31
Hello,

I need to format my value with leading zeros but nagtive sign should appear on left side.

For eg -1.00 should be -00001000.

I am using:
replace(right('0000000000' + convert(varchar(10), convert(decimal(9,3), tonnage)), 10), '.', '') tonnage
but the output is 0000-1000

Please help

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-27 : 13:29:17
what's the datatype of tonnage, and what does the data look like in the table/ or variable?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

sonetav
Starting Member

3 Posts

Posted - 2010-07-27 : 13:40:30
Database datatype is Decimal(9,2) and actual values in db is -1.00
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-07-27 : 13:44:29
I guess there should a better way, but something like this...
declare @t table (tonnage  decimal(9,2))
insert @t
select '-1.00'
union all select '1.00'
union all select '0.00'

select case when tonnage >= 0 
then replace(right('0000000000' + convert(varchar(10), convert(decimal(9,3), tonnage)), 9), '.', '')
else replace('-' + right('0000000000' + substring(convert(varchar(10), convert(decimal(9,3), tonnage)),2,10), 9), '.', '')
end
from @t
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2010-07-27 : 14:11:14



select
a.*,
MyNewNum =
case when MyNum < 0 then '-' else ' ' end+
right('0000000000'+convert(varchar(20),abs(MyNum)),10)
from
( --Test Data
select MyNum = -1.00 union all
select Mynum = 1.00
) a

Results:
MyNum MyNewNum    
----- -----------
-1.00 -0000001.00
1.00 0000001.00

(2 row(s) affected)




CODO ERGO SUM
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-27 : 14:15:35
If possible then you should do that in your front end.

edit: Don't call me madhi

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sonetav
Starting Member

3 Posts

Posted - 2010-07-27 : 14:42:13
Thank you so much it worked smoothly.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-07-28 : 07:04:27
quote:
Originally posted by sonetav

Thank you so much it worked smoothly.


Have you read webfred's reply?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -