Home  >  Article  >  Database  >  Usage of substr function in oracle

Usage of substr function in oracle

下次还敢
下次还敢Original
2024-04-30 08:27:15762browse

The substr() function extracts a substring of a string. The syntax is: substr(str, start, [length]). Usage example: Extract the 4 characters starting from the 3rd character from 'Hello World': SELECT substr('Hello World', 3, 4) FROM dual; Result: 'llo'.

Usage of substr function in oracle

Usage of substr() function in Oracle

The substr() function is used to extract substrings from strings . The syntax is as follows:

<code>substr(str, start, [length])</code>

Where:

  • str: The string from which the substring is to be extracted.
  • start: Extract the starting position of the substring, counting from 1.
  • length (optional): The length of the substring to be extracted. If omitted, everything from the starting position to the end of the string is extracted.

Usage example

<code>SELECT substr('Hello World', 3, 4) FROM dual;</code>

Output:

<code>llo </code>

Example description:

  • This query extracts a 4-character substring starting from the 3rd character ('l') from the string 'Hello World'.
  • The result substring is "llo".

Other usage examples:

  • Extract the first character of the string:

    <code>SELECT substr('Oracle', 1, 1) FROM dual;</code>
  • Extract the last few characters of the string:

    <code>SELECT substr('Database', -3) FROM dual;</code>
  • Extract the substring of the specified length:

    <code>SELECT substr('Programming', 1, 8) FROM dual;</code>

Note:

  • start parameter must be greater than or equal to 1.
  • length parameter must be greater than or equal to 0.
  • If start length > string length, only the characters from start to the end of the string will be extracted.
  • If start or length is negative, NULL will be returned.

The above is the detailed content of Usage of substr function in oracle. 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