Sample Header Ad - 728x90

Converting a number to a varchar, doing an operation, converting it back to a number in MySQL

1 vote
2 answers
54 views
I am trying to solve The Blunder challenge under MySQL database and I am running into an error in what I thought to be a correct answer: `SELECT CEIL(AVG(Salary) - AVG(CAST(REPLACE(CAST(Salary AS VARCHAR(255)), '0', '') AS DECIMAL(14,2)))) AS avg_error FROM Employees;` In plain words, I tried converting the Salary (which is originally of a number type) into a varchar, remove any 0s appearing in it, convert it back to a number. I then calculated the average of the error and took a ceiling of it. However, when I run the code, I get the following error message: `Compiler Message Runtime Error Error (stderr) ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(255)), '0', '') AS DECIMAL(14,2)))) AS avg_error FROM Employees' at line 1 Your Output (stdout) ~ no response on stdout ~` I don't know where I made a mistake. Could you tell me how I can fix this, and if there is another way of a solution?
Asked by love and light (113 rep)
Jul 23, 2024, 04:52 PM
Last activity: Jul 25, 2024, 04:28 PM