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)
 Convert Float to Varchar

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-09-09 : 08:20:06
Hi all,
I try to covert a float value to a VARCHAR data type but it does not convert it completely.
declare @f float
set @f=rand()
select @f
--0.422263491766635
select cast(@f as varchar)
--0.422263


How can I convert it? I want the result be: 0.422263491766635

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-09 : 08:32:20
convert(varchar(20), convert(decimal(20,15), @f))

refer to http://msdn.microsoft.com/en-us/library/ms187928.aspx
quote:

float and real Styles

When expression is float or real, style can be one of the values shown in the following table. Other values are processed as 0.
Value Output

0 (default) - A maximum of 6 digits. Use in scientific notation, when appropriate.

1 - Always 8 digits. Always use in scientific notation.

2 - Always 16 digits. Always use in scientific notation.




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

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-09-09 : 08:35:47
try like this
declare @f float
set @f=rand()
select @f
select convert(numeric(18,16),@f)
select convert(varchar(64),convert(numeric(18,16),@f))

Go to Top of Page
   

- Advertisement -