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
 General SQL Server Forums
 New to SQL Server Programming
 Float to varchar

Author  Topic 

velliraj
Yak Posting Veteran

59 Posts

Posted - 2010-07-19 : 08:19:20
While working on one Data type conversion activity in SQL Server, we came across one scenario where we need to take value from one column having data type as Varchar, and use that column in function (where Datatype for that column would be Float for Arithmatic calculation) and thereafter populate the calculated value again back to that column with datatype varchar.

So the case is Varchar to Float and again Float to Varchar conversion for that specific column.

The Decimal places can not be bounded as per the business constraints. therefore we cant use decimal conversion. below example will demonstrate the scenario.

Is there any way to get the accurate values back in to the column after all the calculation. Please suggest.


declare @value varchar(100)
set @value= '4567623.234455'

declare @newValue Float
Set @NewValue= @Value

select @NewValue as ValueASFloat
select convert(varchar(255),@NewValue) as ValueASVarchar
select Cast( @NewValue as Decimal(20,9)) as ValueASDecimal

Please any one help....

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-07-19 : 08:48:10
Try this


select @NewValue as ValueASFloat
select @NewValue+'' as ValueASVarchar
select Cast( @NewValue as Decimal(20,6)) as ValueASDecimal

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-07-19 : 08:50:46
declare @value varchar(100)
set @value= '4567623.234455'

declare @newValue float-- decimal(16,8)
Set @NewValue= @Value

select @NewValue as ValueASFloat
select convert(varchar(255),@NewValue) as ValueASVarchar
select Cast( @NewValue as Decimal(20,6)) as ValueASDecimal


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

velliraj
Yak Posting Veteran

59 Posts

Posted - 2010-07-19 : 09:35:06
Hi madhi,

Thanks for your inputs.

I need to get the output, same as float value even after storing in the varchar column. using decimal function is restricting decimal points.
we dont want to use the decimal function.


Go to Top of Page
   

- Advertisement -