Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the first character in a field

Tags:

mysql

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!

like image 821
seb Avatar asked Jun 23 '11 23:06

seb


People also ask

How do I change the first character in MySQL?

Use the MySQL REPLACE() function to replace a substring (i.e. words, a character, etc.) with another substring and return the changed string.

How do you remove the first character of a string in SQL?

To delete the first characters from the field we will use the following query: Syntax: SELECT SUBSTRING(string, 2, length(string));

What is left in MySQL?

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.


2 Answers

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 |
+-----------+
like image 184
cEz Avatar answered Sep 22 '22 06:09

cEz


update customers_basket 
set products_id = 'S' + SUBSTRING(products_id,2,CHAR_LENGTH(products_id))
where LEFT(products_id,1) = 'U'
like image 30
Petar Ivanov Avatar answered Sep 19 '22 06:09

Petar Ivanov