I have thousands of rows that contain a zipcode attribute with four integers in.
Some rows contain five integers, and some other contain two, or three integers.
What I want to do is to add a 0 before rows that contain only four integers.
The tablename is zipcode and the attribute too.
I want to update the rows and not show them !
I tried this code with no luck (no row affected) :
UPDATE zipcode
SET zipcode = CONCAT('0', zipcode)
WHERE CHAR_LENGTH(zipcode) = 4
Btw, I can use CHAR_LENGTH in a simple select, and it works :
SELECT *
FROM zipcode
WHERE CHAR_LENGTH(zipcode) = 4
So, seems that the CHAR_LENGTH doesn’t work in my WHERE clause.
EDIT: zipcode is an int.
It sounds like you are storing the zipcode as an int, but you really want to treat it as a char.
In that case you have a couple of options:
1) Change the datatype of the column and then execute your update to left pad with 0:
2) Leave the datatype as is, and just format the data on retrieval by left-padding with 0 in the select statement:
Demo