SQL convert varchar column to float

后端 未结 1 1998
谎友^
谎友^ 2021-01-19 10:31

An importing tool put every column in several tables as varchar(max) and I am looking for the fastest way to convert the columns to the proper type.

This works fine,

相关标签:
1条回答
  • 2021-01-19 11:04

    Are the decimal separator of the system configured the same way as content from varchar field? In your case i will advice you to créate a new column(FLOAT) instead of alter existing column. Then fill that field with an update.

    Here is a way to explicit convert text to float. IN SQL Server this query :

    select cast(replace('12,5',',','.') as float)*2
    

    Resutns 25 as response. That means is converted successfull to FLOAT

    So to fill you new col with casted values you could do:

    UPDATE Table SET FloatField=CAST(REPLACE(TextField,',','.') AS FLOAT)
    

    That replace the , for . (In case there is any) and then converts to FLOAT. Hope this help.

    0 讨论(0)
提交回复
热议问题