Home> Database> SQL> body text

How to use replace function in sql

下次还敢
Release: 2024-05-02 04:21:16
Original
673 people have browsed it

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).

How to use replace function in sql

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

REPLACE(string, old_substring, new_substring)
Copy after login

Parameters

  • string:Need to replace the substring text or string.
  • old_substring:The substring to be replaced from string.
  • new_substring:New substring that replaces old_substring.

Usage

  1. Basic usage:Replace all occurrences of substrings in a text or string.
SELECT REPLACE('This is a sample text', 'sample', 'example');
Copy after login

Result: This is an example text

  1. Conditional replacement:Replace substrings based on given conditions.
SELECT REPLACE('This is a sample text', 'sample', 'example', 1);
Copy after login

Result: This is an example text

where 1 specifies that only the first occurrence of the substring is replaced.

  1. NULL value handling:The REPLACE function supports NULL values. If old_substring or new_substring is NULL, NULL is returned.
SELECT REPLACE('This is a sample text', NULL, 'example');
Copy after login

Result: NULL

Notes

    ##The REPLACE function is case-sensitive.
  • If old_substring does not exist in string, no replacement is performed.
  • The REPLACE function does not modify the original data. It returns a copy containing the replaced data.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!