SUBSTRING_INDEX
Description
The SUBSTRING_INDEX function is used to extract the substring before the delimiter delim appears count times in a string or binary data. This function is very useful when processing text data, especially when you need to split or extract strings based on a specific delimiter.
Parameter Description
- expr (string/binary): The original string or binary data to be processed.
- delim (string/binary): The string or binary data used as the delimiter, of the same type as expr.
- count (bigint): Indicates the count of the delimiter's occurrences. If count is positive, counting starts from the left side of the string; if count is negative, counting starts from the right side; if count is 0, an empty string is returned.
Return Result
Returns a string or binary data representing the substring before the delimiter appears count times in expr.
Usage Example
Notes
- When count is a positive number, the function will start searching for the delimiter delim from the left side of the string and return the substring before it.
- When count is a negative number, the function will start searching for the delimiter delim from the right side of the string and return the substring before it.
- When the absolute value of count is greater than the number of occurrences of the delimiter, the function will return an empty string.
- If expr or delim is NULL, it returns NULL.