views:

79

answers:

2

I have a float column with numbers of different length and I'm trying to convert them to varchar.

Some values exceed bigint max size, so I can't do something like this

cast(cast(float_field as bigint) as varchar(100))

I've tried using decimal, but numbers aren't of the same size, so this doesn't help too

CONVERT(varchar(100), Cast(float_field as decimal(38, 0)))

Any help is appreciated.

UPDATE:

Sample value is 2.2000012095022E+26.

+1  A: 

Try using the str() function.

Select Str(float_field, 25, 5)

Str() Function

Barry
I got ************************* . What's that? :)
hgulyan
@hgulyan - Does `Select LTRIM(Str(float_field, 38, 0))` work for your data?
Martin Smith
@Martin Smith, It seems to work, but the same way as decimal, so I'm not sure if it's the real value(last ten digits are zero). I guess, that the real value was lost. Thank you!
hgulyan
@hgulyan -the last ten digits are zero because that is what the last parameter is for in the `Str` function. The number of digits after the decimal point. Did you read the link i posted? Change the zero to 10. `Select LTRIM(Str(float_field, 38, 10))`
Barry
+1  A: 

float only has a max. precision of 15 digits. Digits after the 15th position are therefore random, and conversion to bigint (max. 19 digits) or decimal does not help you.

devio
I don't get it. Field value is 2.2000012095022E+26. What's the solution? There isn't any?
hgulyan
you cannot get more digits by converting to string than there are digits stored in the original value.
devio
So I've lost digits after 15th position?
hgulyan
There was some kind of problem with the data. I just need to update that value with a 15 digit float. I'll accept your answer, because it describes main problem I had with this data. Thank you.
hgulyan