|
Custom Search
| |
|
How to use MySQL REPLACE function in SELECT and UPDATE queries MySQL REPLACE function replaces all occurrences of a string inside another string. Basic syntax: REPLACE(str, from, to)
REPLACE() performs a case-sensitive search and replacement. Examples 1. Query below replaces all occurrences of the upper case string "GPT" with the string "XXX", but lowercase "gpt" is not replaced because REPLACE() function is case-sensitive. -- Result: ChatXXX is a smart XXX but is not gpt. select replace("ChatGPT is a smart GPT but is not gpt.", "GPT","XXX"); 2. Query below searches the word "Manager" in column ContactTitle in suppliers table, and replace it with "Managing Director". Note that this is a SELECT query and the actual values in the column are not changed.
select CompanyName, ContactTitle as OldTitle, replace(ContactTitle, "Manager","Managing Director") as NewTitle from suppliers order by SupplierID;
29 records returned: 3. Query below updates the word "Manager" to "Managing Director" in ContactTitle column in suppliers table when CompanyName is "Tokyo Traders". Note that this is an UPDATE query and the actual values in the column are changed. Make sure you make a backup of the table before the update. Update suppliers set ContactTitle = replace(ContactTitle, "Manager","Managing Director") where CompanyName = "Tokyo Traders";
1 record was updated:
Happy Coding! Other tutorials in this category 1. Using String Functions, Part 1 |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |