The REPLACE function in SQL is used to replace all given substrings with another substring in a text or string. Can be used for basic replacement, conditional replacement, and NULL value handling. The basic syntax is: REPLACE(string, old_substring, new_substring).
Usage of REPLACE function in SQL
The REPLACE function is used to replace all given values in a text or string. A given substring is another substring. It can update data in the database based on specified conditions.
Syntax
<code>REPLACE(string, old_substring, new_substring)</code>
Parameters
Usage
<code>SELECT REPLACE('This is a sample text', 'sample', 'example');</code>
Result: This is an example text
<code>SELECT REPLACE('This is a sample text', 'sample', 'example', 1);</code>
Result: This is an example text
where 1 specifies that only the first occurrence of the substring is replaced.
<code>SELECT REPLACE('This is a sample text', NULL, 'example');</code>
Result: NULL
Notes
The above is the detailed content of How to use replace function in sql. For more information, please follow other related articles on the PHP Chinese website!