I have looked at all error 1292 Truncated Error Double Value questions, but have not been able to derive a solution from them.
Answers have however led me to an assumtion that I am failing to prove:
I am querying MYSQL out of NODEJS using the MYSQL2 library. My table named shoptableplayground contains a column/field named regular_price. It has the data type attribute VARCHAR(45).
I would like to increase the value of said column/field by 10% for all rows.
I am getting error 1292 from the following query:
UPDATE shoptableplayground
SET regular_price = regular_price * 1.1 WHERE regular_price BETWEEN 1 AND 200;
I am assuming that I am getting the error because WHERE expects to compare numerical data types (DEC, INT etc.) in order to be able to do the actual comparison.
My various attempts of incoorporating CONVERT or CAST or even ALTER TABLE MODIFY COLUMN datatype all lead to MYSQL SYNTAX ERRORS. Unfortunately most tutorials cover either SELECT or some form of direct data manipulation.
I’ve also tried rebuilding the table with a DECIMAL(10.2) column for column regular_price, but the populating process (outside my control) seems to insert the Data as string / VARCHAR nevertheless.
Any query suggestion on how to solve the issue would be much appreciated.
2
Answers
First of all, It’s a big mistake to store a calculated number as a VARCHAR type, but we’ll ignore it for now.
Focus, you have a column contains many rows filled by prices(floats) but it is considered as a VARCHAR column we cannot do calculations on it.
The Trick is Casting two times :
Type to The Column Type(VARCHAR)
also you will cast the condition the ensure the interval of the price(between 0 and 200)
Edit :
Since you use the comma (,) as a decimal seperator and the MySQL expects periods (.) as decimal seperators, you should replace comma by periods.
you can’t keep working with this database and you don’t follow the rules.
So first you need to change the column type to be float and following the US/English Notation of numbering and changing the old rows to to comply with the new type.
and to stay out of risk, we will create a new column to hold the successfully converted rows, then we will remove the original row and renaming the new column as if nothing has changed.
apply these queries separately, if one failed don’t apply the next until you resolve the first query problem