SUBSTRING
Description
The SUBSTRING function is used to extract a substring from a string or binary data. Based on the specified starting position (pos) and length (len), the SUBSTRING function returns the corresponding substring.
Syntax
Parameters
str
(string/binary): The input string or binary data.pos
(bigint): The starting position of the substring. If pos is greater than or equal to 1, it starts from the pos-th character from the left; if pos is less than or equal to -1, it starts from the -pos-th character from the right; if pos is equal to 0, it starts from the first character on the left.len
(bigint, optional): The length of the substring. If not specified, the complete substring starting from pos is returned.
Return Result
Returns a string representing the substring extracted from the input string.
Usage Example
- Extract the first two characters of the string:
- Extract the complete substring starting from the fourth character:
- Extract the two characters starting from the second to last character of the string:
- Extract the domain name from the URL:
- Extract specific parts of a string (e.g., extract month and date):
By the above examples, you can see the application of the SUBSTRING function in different scenarios. Using the SUBSTRING function allows you to conveniently extract the required substring from a string, thereby meeting various data processing needs.