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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem converting data

Status
Not open for further replies.

Jenns

Programmer
Nov 1, 2000
36
US
Hi,

I've got a varchar field that has decimal data in it. For example: 20.00, 120.00, 1120.00. I need to get the data to look like 20, 120, 1120 to link it up to ID numbers in another table. I tried converting the data to float, int and numberic, but each time it creates an error. Does anyone have any suggestions?

Thanks!
Jenn
 
Code:
declare @a as varchar(250)
select @a = '1120.00'

select cast(cast (@a as float) as int)

--however with the '.' on the end u would need to 
select @a = '1120.00.'

select  
case when right(@a,1) = '.' then 
		cast(cast (left(@a ,len(@a)-1) as float) as int)
     else
		cast(cast (@a as float) as int)
end
 
Thanks for the help. Your code works just fine, but when I subsitute my field name for your variable I still get "Error converting data type varchar to float".


select cast(cast (myField as float) as int) as myField
from myTable.

Why could it convert one way and not the other?

Thanks!
Jenn
 
Ah huh! I found out why. There are commas in there too. Rats! Now how do I handle that?

Thanks!
Jenn
 
Since you are starting with a varchar field, you can replace the commas with nothing.

select cast(cast (Replace(myField, ',', '') as float) as int) as myField
from myTable.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
That works perfectly! Thanks so much!

Thanks!
Jenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top