Please see the DDL below:
create table #TestFloat (id int, floattest float)
insert into #TestFloat values (1,65478)
insert into #TestFloat values (1,65478888)
insert into #TestFloat values (1,42)
How would I cast the float values to a varchar? I have tried this:
select top 1 cast(floattest as varchar(15)) from #TestFloat
which produces this:
6.54789e+007
and this:
select top 1 id,Str(floattest, 15, 0) from #TestFloat
' 65478'
I have put quotes in the above to demonstrate that it is casted into a char.
This works (taken from this question: How to convert Float to Varchar in SQL):
select CAST(CAST(floattest AS DECIMAL(20)) AS VARCHAR(20)) from #TestFloat
Why do I have to cast into a decimal first?