substring
Description
Syntax:
VARCHAR substring(VARCHAR str, INT pos[, INT len])
If len
is not specified, this function will return a substring of str
starting from pos
. If len
is specified, it will return a substring of str
of length len
starting from pos
. pos
can be negative, if so, the returned substring will be pos
characters at the end of str
instead of the beginning. If pos
is 0, it will return an empty string.
For all forms of SUBSTRING(), the position of the first character is 1.
Example
mysql> select substring('abc1', 2);
+-----------------------------+
| substring('abc1', 2) |
+-----------------------------+
| bc1 |
+-----------------------------+
mysql> select substring('abc1', -2);
+-----------------------------+
| substring('abc1', -2) |
+-----------------------------+
| c1 |
+-----------------------------+
mysql> select substring('abc1', 0);
+----------------------+
| substring('abc1', 0) |
+----------------------+
| |
+----------------------+
mysql> select substring('abc1', 5);
+-----------------------------+
| substring('abc1', 5) |
+-----------------------------+
| NULL |
+-----------------------------+
mysql> select substring('abc1def', 2, 2);
+-----------------------------+
| substring('abc1def', 2, 2) |
+-----------------------------+
| bc |
+-----------------------------+
Keywords
SUBSTRING, STRING