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_id, 2, CHAR_LENGTH(products_id)
);
but this does not give me the desired effect, can any one help me?
thank you!
Use the MySQL REPLACE() function to replace a substring (i.e. words, a character, etc.) with another substring and return the changed string.
To delete the first characters from the field we will use the following query: Syntax: SELECT SUBSTRING(string, 2, length(string));
LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With