mysqlsqlmysql-error-1292

Error Code: 1292 - Truncated Incorrect DOUBLE value: '-'


SET @newNum = IF(
    RIGHT("-600.00",1) REGEXP '^[-]' = '-',
    REPLACE("-600.00",",",""),
    -1*REPLACE(REPLACE("-600.00",",",""),"-","")
);

I keep getting a truncated incorrect DOUBLE value: '-' when executing this line, anyone know what is causing this ?


Solution

  • REGEXP returns 1 or 0 depending on whether there is a match or not. Comparing the result with - is incorrect, you can do this instead:

    DECLARE @oldNum VARCHAR(10);
    DECLARE @newNum VARCHAR(10);
    SET @oldNum = '600.00-';
    SET @newNum = IF(
        RIGHT(@oldNum, 1) = '-',
        CONCAT('-', REPLACE(REPLACE(@oldNum, ',', ''), '-', '')),
                            REPLACE(@oldNum, ',', '')
    );
    SELECT @oldNum, @newNum