Home> Database> SQL> body text

How to replace characters at specified positions in sql

下次还敢
Release: 2024-05-08 11:33:16
Original
618 people have browsed it

In SQL, use the SUBSTR() function to specify the starting position and length of the character to be replaced, and then use the REPLACE() function to replace the character at the specified position. The syntax is REPLACE(string, start, length, new_string) .

How to replace characters at specified positions in sql

Replace the character at the specified position in SQL

In SQL, you can use the SUBSTR() function and REPLACE( ) function to replace the character at the specified position.

Syntax

REPLACE(string, start, length, new_string)
Copy after login

Parameters

  • string: The original character of the character to be replaced string.
  • start: The starting position of the character to be replaced (counting from 1).
  • length: The length of the characters to be replaced.
  • new_string: String used for replacement.

Example

Replace the 3 characters starting from the 5th position in the string to "ABC":

SELECT REPLACE('Hello World', 5, 3, 'ABC');
Copy after login

Output:

Hello ABCld
Copy after login
Copy after login

SUBSTR() function

SUBSTR() function can be used to specify the starting position and length of the characters to be replaced:

Syntax

SUBSTR(string, start, length)
Copy after login

Parameters

  • string: The original string from which the substring is to be extracted.
  • start: The starting position of the substring to be extracted (counting from 1).
  • length: The length of the substring to be extracted.

Example

Use the SUBSTR() function to specify the substring to be replaced:

SELECT REPLACE('Hello World', SUBSTR('Hello World', 5, 3), 'ABC');
Copy after login

Output:

Hello ABCld
Copy after login
Copy after login

The above is the detailed content of How to replace characters at specified positions 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!