Home  >  Article  >  Database  >  How to replace characters at specified positions in sql

How to replace characters at specified positions in sql

下次还敢
下次还敢Original
2024-05-08 11:33:16732browse

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

<code>REPLACE(string, start, length, new_string)</code>

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":

<code>SELECT REPLACE('Hello World', 5, 3, 'ABC');</code>

Output:

<code>Hello ABCld</code>

SUBSTR() function

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

Syntax

<code>SUBSTR(string, start, length)</code>

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:

<code>SELECT REPLACE('Hello World', SUBSTR('Hello World', 5, 3), 'ABC');</code>

Output:

<code>Hello ABCld</code>

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!

Statement:
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