Jump to: navigation, search

Mysql SUBSTR

From w3cyberlearnings

Contents

MySQL SUBSTR Function

This function returns a substring from a given string. It is equal to SUBSTRING() functions.

Syntax SUBSTR

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

OR

SUBSTR(Str FROM pos FOR length);

Example 1

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


Example 2

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

Example 3

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

Example 4

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

Example 5

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

Example 6

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

Related Links


Navigation
Web
SQL
MISC
References