Jump to: navigation, search

Mysql SUBSTRING

From w3cyberlearnings

Contents

MySQL SUBSTRING Function

This function returns a substring from a given string.

Syntax SUBSTRING

  • Str: A given string
  • Pos: A position to start
  • length (optional): length
SUBSTRING(Str, pos, length);

OR

SUBSTRING(Str FROM pos FOR length);

Example 1

mysql> SELECT SUBSTRING('Hello Word',6);
+---------------------------+
| SUBSTRING('Hello Word',6) |
+---------------------------+
|  Word                     |
+---------------------------+
1 row in set (0.00 sec)


Example 2

mysql> SELECT SUBSTRING('12345678',6);
+-------------------------+
| SUBSTRING('12345678',6) |
+-------------------------+
| 678                     |
+-------------------------+
1 row in set (0.00 sec)

Example 3

mysql> SELECT SUBSTRING('12345678',3,4);
+---------------------------+
| SUBSTRING('12345678',3,4) |
+---------------------------+
| 3456                      |
+---------------------------+
1 row in set (0.00 sec)

Example 4

mysql> SELECT SUBSTRING('12345678',-2);
+--------------------------+
| SUBSTRING('12345678',-2) |
+--------------------------+
| 78                       |
+--------------------------+
1 row in set (0.00 sec)

Example 5

mysql> SELECT SUBSTRING('12345678',-3,2);
+----------------------------+
| SUBSTRING('12345678',-3,2) |
+----------------------------+
| 67                         |
+----------------------------+
1 row in set (0.00 sec)

Example 6

mysql> SELECT SUBSTRING('12345678' FROM -3 FOR 2);
+-------------------------------------+
| SUBSTRING('12345678' FROM -3 FOR 2) |
+-------------------------------------+
| 67                                  |
+-------------------------------------+
1 row in set (0.00 sec)

Related Links


Navigation
Web
SQL
MISC
References