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)
 CAST+SPACE+CONVERT=confuse

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-24 : 04:27:47
[code]DECLARE @total DECIMAL(21,8)
SET @total = 0.111

PRINT '|'+SPACE(21-LEN(@total)) + RTRIM(CAST(CONVERT(DECIMAL(21,2), @total) AS CHAR(21)))+'|'
RESULT
| 0.11|
X123456789012345X

PRINT '|'+CAST(CONVERT(DECIMAL(21,2), @total) as char(21))+'|'
RESULT
|0.11 |
X123456789012345678901X

as you can see, i was trying to align right but i can't figure out how X.x
DESIRE OUTPUT
| 0.11|
X123456789012345678901X[/code]

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-24 : 04:32:31
[code]right(space(21) + convert(varchar(21), @total), 21)[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-24 : 04:35:45
PRINT '|'+SPACE(21-LEN(CONVERT(DECIMAL(21,2),@total))) + RTRIM(CAST(CONVERT(DECIMAL(21,2), @total) AS CHAR(21)))+'|'
solved =.="""
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-24 : 04:48:41
Print '|'+right(space(21) + convert(varchar(21), @total), 21)+'|'
Result

| 0.11100000|
X123456789012345678901X
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-24 : 04:53:46
oh din't realize @total is of diff precision. Just convert to decimal(21,2) before converting to varchar() should do the trick


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-24 : 05:07:55
Yours solution always da best ahhaha
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 06:13:02
Huh? Why overcomplicate things?
PRINT '|' + STR(@Total, 15, 2) + '|'


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-24 : 10:39:43
wow!!!! omg the shortest
Go to Top of Page
   

- Advertisement -