Translate

Monday, December 26, 2016

MYSQL How to prepend a string when updating a column




In order to prepend a string when you are updating a columns is kind of easy, you only need to know the right Mysqsl function to achieve that.

Example:

For any reason, someone requested you to update the user user_login for the user ID:999  with this symbol "@" prefix to each name:

   

update users 
set user_login = concat('@', user_login)
where id = 999;


 



As you can see the function "concat" is prepending the @ to each user login.

The expected output would be


ID user_login
999 @john.doe






It is important to note that Concat allows you to add a string either at the end or at the beginning.