Skip to content

SUBSTRING_INDEX

  • str: This represents the source string from which a substring is to be extracted.
  • delim: This refers to the delimiter which is used to divide the source string. It identifies the boundary between sequential units in the string.
  • count: This denotes the number of occurrences of the delimiter in the source string. If the value of count is positive, everything to the left of the final delimiter (counting from the left) is returned. If the value of count is negative, everything to the right of the final delimiter (counting from the right) is returned.
SELECT SUBSTRING_INDEX('www.google.com', '.', 2);
'www.google'

In this example, the SUBSTRING_INDEX() function is used to return a substring from a string before counting a specified number of delimiter occurrences. The string ‘www.google.com’ is used as the input, ’.’ is specified as the delimiter and 2 as the count. Consequently, this results in ‘www.google’.