Purpose
This function is used to extract a substring from a string. Multibyte characters (such as Chinese characters and full-width characters) are counted as one character.
Syntax
SUBSTR(char1,n1[,n2])
Parameters
Parameter |
Description |
|---|---|
| char1 | The string to be extracted. The string can be of the CHAR, VARCHAR2, NCHAR, NVARCHAR2, or CLOB type. |
| n1 | The starting position of the substring to be extracted. The data type is NUMBER. Description
|
| n2 | The length of the substring to be extracted. The data type is NUMBER, and this parameter is optional. Description
|
Return type
The same data type as char1.
Examples
Extract 5 characters starting from the 1st character and 2.1 characters starting from the -3rd character of the string OceanBase.
obclient> SELECT SUBSTR('OceanBase',1,5) "SUBSTR1",SUBSTR('OceanBase',-3,2.1) "SUBSTR2" FROM DUAL;
+---------+---------+
| SUBSTR1 | SUBSTR2 |
+---------+---------+
| Ocean | as |
+---------+---------+
1 row in set
