mysql error 1292 when using cast in update statement

前端 未结 3 2111
南笙
南笙 2021-01-22 11:56

The below statement returns \"Error Code: 1292. Truncated incorrect INTEGER value: \'95.00\' 1.132 sec \"

update new2006 set new2006.emp=cast(emp as unsigned)          


        
3条回答
  •  自闭症患者
    2021-01-22 12:10

    It's easy to reproduce, e.g.:

    CREATE TABLE new2006 (
     emp VARCHAR(7)
    );
    INSERT INTO new2006 (emp) VALUES ('95.00');
    UPDATE new2006 SET emp=CAST(emp AS UNSIGNED);
    

    I suspect the problem is related to internal conversions from/to base 2 because there's no error message when you cast to DECIMAL:

    UPDATE new2006 SET emp=CAST(emp AS DECIMAL);
    

    ... or when you remove the decimals from source data:

    INSERT INTO new2006 (emp) VALUES ('95');
    

    I couldn't find anything in the manual page for CAST() that sheds light on the subject. My guess is that MySQL chooses different algorithm or internal variable types depending on the input scenario and sometimes truncation happens while performing internal calculations.

提交回复
热议问题