开发者

SQL Query Question: Select all rows of n length, and then append a character

开发者 https://www.devze.com 2022-12-16 10:21 出处:网络
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 mi

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消