MySQL REPLACE()

The REPLACE() function replaces all occurrences of a specified substring within a string with another substring. It is useful for modifying text data.

Examples

1. Replacing a Substring

SELECT REPLACE('Raja Raja Chola', 'Chola', 'The Great') AS modified_text;

Code Explanation: This query replaces 'Chola' with 'The Great', resulting in 'Raja Raja The Great'.

2. Using REPLACE with Column Data

SELECT king_name, REPLACE(king_name, 'Chola', 'Empire') AS modified_name FROM tamil_kings_auto_increment;

Code Explanation: This query replaces 'Chola' with 'Empire' in the king_name column for all records.

Best Practices

  • Use REPLACE() to update text data with new values.
  • Be careful with case sensitivity, as REPLACE() is case-sensitive.

Key Takeaways

  • The REPLACE() function replaces all occurrences of a substring with another substring.
  • It is useful for text modifications and data cleaning.