SUBSTR
Description
The SUBSTR function is used to extract a substring of specified position and length from a string or binary data.
Syntax
or
Parameter Description
str(string/binary): Input string or binary data.pos(bigint): Starting position of the substring. Ifposis greater than or equal to 1, extraction starts from thepos-th character from the left; ifposis less than or equal to -1, extraction starts from the-pos-th character from the right; ifposis equal to 0, extraction starts from the first character on the left.len(bigint, optional): Length of the substring to extract. If not specified, the full substring starting fromposis returned.
Return Result
Returns the extracted substring, type is string.
Example
- Extract 5 characters from the string "Hello, world!" starting from the second character:
- Extract all characters from the fourth character onwards from the string "123456789":
- Extract 1 character from the string "数据库" starting from the second character, and specify the character set as UTF-8:
Notes
- If the value of
posorlenexceeds the range of the input string, the function will return an empty string. - When extracting a string, the units for
posandlenare characters, not bytes.
