开发者

change the first character in a field

I wish to change the first character from a 'U' to an 'S' in a field in my table (MYSQL) , so far I have the following :

UPDATE customers_basket 
SET products_id = REPLACE(
                   LEFT(products_id,1), 'U', 'S') +      
                   SUBSTRING(products_i开发者_运维技巧d, 2, CHAR_LENGTH(products_id)
);

but this does not give me the desired effect, can any one help me?

thank you!


UPDATE customers_basket 
SET products_id = CONCAT(
                   REPLACE(
                      LEFT(products_id,1), 'U', 'S'),      
                      SUBSTRING(products_id, 2, CHAR_LENGTH(products_id)
));

You are trying to add characters together, e.g.

select 'c' + 'a';
+-----------+
| 'c' + 'a' |
+-----------+
|         0 |
+-----------+


update customers_basket 
set products_id = 'S' + SUBSTRING(products_id,2,CHAR_LENGTH(products_id))
where LEFT(products_id,1) = 'U'


You could simply use:

UPDATE customers_basket SET products_id=CONCAT('S', SUBSTRING(products_id FROM 2));

i.e.: Instead of replacing the initial "U" with an "S", simply start with an "S" and copy the remaining characters.

This of course presumes that all of the products_id entries begin with a "U". If they don't, simply add a WHERE condition such as:

UPDATE customers_basket SET products_id=CONCAT('S', SUBSTRING(products_id FROM 2))
WHERE LEFT(products_id, 1) = 'U';


SUBSTRING(products_id, 2) will give you the second character onwards. So you can do something like:

update customers_basket set products_id = concat( 'S', substring(products_id, 2));

If you only want to change those whose first character is a 'U' just add an appropriate where clause, e.g.

update customers_basket set products_id = concat( 'S', substring(products_id, 2)) where left(products_id,1)='U';


update customers_basket 
set products_id = CONCAT('S', RIGHT(products_id,CHAR_LENGTH(products_id)-1))
WHERE LEFT(products_id,1) = 'U'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜