Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

numeric to char

Status
Not open for further replies.

aspnet98

MIS
May 19, 2005
165
US
I need to convert a numeric field to char.

Numeric field:
1.1
0.5
-1.4

to char
1.1
0.5
-1.4


I keep getting
1.10
0.50
-1.40 using CAST(FIELD AS char(9))???

Any ideas on this?

 
This is probably happening because of the way the numeric is defined. By first casting your numeric value as real, and then to varchar, you should be able to circumvent this problem.

Run this in QA to better understand what I am talking about.

Code:
Declare @N1 NUmeric(10,2)
Declare @N2 Numeric(10,1)

Set @N1 = 5.8
Set @N2 = 5.8

Select Cast(@N1 As Char(9)), 
       Cast(@N2 As Char(9)), 
       Cast(Cast(@N1 As Real) As VarChar(9))

Hope it helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top