0

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.
2
  • Numbers don't have commas. Remove those first using REPLACE. Commented Aug 23, 2019 at 11:53
  • sql servers uses a dot . in stead of a comma , for floats, so you must replace these first Commented Aug 23, 2019 at 11:57

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.