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 2000 Forums
 SQL Server Development (2000)
 Convert Real To NVARCHAR

Author  Topic 

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2002-10-15 : 14:23:44
SELECT CONVERT(REAL,0)

This will result in 0.0

Is there a way using cast, convert or a string function to

return a string "0.0"

My current work around for this is using the FormatNumber function in VB.

NEVER MIND STR() WORKS I was not using it right

SOLUTION:
SELECT STR(CONVERT(REAL,0),3,1)

GENERIC:
SELECT LTRIM(STR(CONVERT(REAL,@value),@sizeofstring,@numdecimals)






Edited by - ValterBorges on 10/15/2002 14:39:35

Edited by - ValterBorges on 10/15/2002 14:48:40

Crespo

85 Posts

Posted - 2002-10-16 : 05:36:27
I was about to suggest using STR() but then I noticed that you have actually answered your own question?!

I faced a similar problem a few days ago, but the STR() function solved the problem.

The interesting thing is, when using the STR() function on its own, in my case : LTRIM(STR((CAST(B.ORIGMBRPEN2 AS NUMERIC)/100), 9, 2))
without the LTRIM, the function returns the value as a string but with blanks, hence the use of the LTRIM. Any ideas why it does that? (I think it has 4 or 5 blanks before the value).

Good Luck!

Crespo.
Hewitt Bacon & Woodrow
Epsom
Surrey
United Kingdom
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-10-16 : 08:44:21
Yes, that's normal. It will create a numeric string with a total length of 9, 2 of which are decimal places. If the number is shorter it will pad the left side with spaces to make up the full length. This allows numbers to be right-aligned in the query output. You can either LTRIM them if you don't need the spaces, or use REPLACE() to change the spaces to leading zeros.

Go to Top of Page

Crespo

85 Posts

Posted - 2002-10-16 : 08:50:59
Thanks Rob!

D'oh! I should have realised that to be honest! .

Anyway... your answer is much appreciated.

Good Luck!

Crespo.
Hewitt Bacon & Woodrow
Epsom
Surrey
United Kingdom
Go to Top of Page
   

- Advertisement -