Jump to: navigation, search

Mysql SUBSTRING INDEX

From w3cyberlearnings

Contents

MySQL SUBSTRING_INDEX Function

This function return a substring from a given string.

Syntax SUBSTRING_INDEX

  • Str: A String input
  • Delimiter: Delimiter within a string
  • Count: count the occurrence of the delimiter
    • If count is negative value (-), search delimiter from the end of the string.
    • If count is positive value (+), search delimiter from the beginning of the string.
SUBSTRING_INDEX(Str, delimiter, count);

Example 1

mysql> SELECT SUBSTRING_INDEX('www.w3cyberlearnings.com','.',2);
+---------------------------------------------------+
| SUBSTRING_INDEX('www.w3cyberlearnings.com','.',2) |
+---------------------------------------------------+
| www.w3cyberlearnings                              |
+---------------------------------------------------+
1 row in set (0.00 sec)


Example 2

mysql> SELECT SUBSTRING_INDEX('www.w3cyberlearnings.com','.',-2);
+----------------------------------------------------+
| SUBSTRING_INDEX('www.w3cyberlearnings.com','.',-2) |
+----------------------------------------------------+
| w3cyberlearnings.com                               |
+----------------------------------------------------+
1 row in set (0.00 sec)

Example 3

mysql> SELECT SUBSTRING_INDEX('girl goes to school, and goes to market','to',2);
+-------------------------------------------------------------------+
| SUBSTRING_INDEX('girl goes to school, and goes to market','to',2) |
+-------------------------------------------------------------------+
| girl goes to school, and goes                                     |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)


Related Links


Navigation
Web
SQL
MISC
References