SQL Query Question: Select all rows of n length, and then append a character
I have a ZIP code column where some of the zips came in without the leading zero.
My goal is to:
1) select all rows in the zip column that are four characters in length (e.g. the zip code entries missing a zero)
and then
2) append a '0' to these columns.
I am able to select the rows:
SELECT zip FROM contact WHERE zip LIKE "____";
and 开发者_如何学CI found on this site how to append the '0':
UPDATE contact SET zip = Concat('0', zip);
but how do I combine them both together into one query?
Thanks.
Like this:
UPDATE contact
SET zip = Concat('0', zip)
WHERE zip LIKE "____";
You can also do it like this:
UPDATE contact
SET zip = Concat('0', zip)
WHERE LEN(zip) = 4;
精彩评论