Error when I try to convert string column to float column

I have a table with a column A. I would like to change the datatype from string to float. My column A looks like this:

A
143
1,440
19,630
12

... When I try to run my code:

ALTER TABLE [Table1]
ALTER COLUMN [A] FLOAT

I always get this error:

Error converting data type varchar to float.

Upvotes: 0

Views: 982

Answers (1)

DarkRob
DarkRob

Reputation: 3833

It is clear in error that varchar value is not converting into float. Since , is not allowed in int, float, decimal type values, so first need to replace your , .

Instead of this you may try this.

  update table set A = Replace( A, ',', '' )

Or

  update table set A = Replace( A, ',', '.' )

Whichever condition suits you better.

After that convert your column.

  ALTER TABLE [Table1]
  ALTER COLUMN [A] FLOAT

Upvotes: 1

Related Questions