Skip to main content
Version: V1.0.0

SUBSTR

Syntax

SUBSTR(str, pos)
SUBSTR(str, pos, len)
SUBSTR(str FROM pos)
SUBSTR(str FROM pos FOR len)

Description

Returns a substring of str starting at position pos and with length len. If any of the parameters is NULL, the function returns NULL.

  • If len is not specified, the substring starts at position pos and continues to the end of str.

  • If pos is a negative number, the starting position is determined by counting backward from the end of str.

  • If len is less than or equal to 0, or if the starting position specified by pos is invalid, the function returns an empty string.

Examples

SELECT
SUBSTR('abcdefg', 3),
SUBSTR('abcdefg', 3, 2),
SUBSTR('abcdefg', -3),
SUBSTR('abcdefg', 3, -2),
SUBSTR('abcdefg' from -4 for 2)
\G
*************************** 1. row ***************************
SUBSTR('abcdefg', 3): cdefg
SUBSTR('abcdefg', 3, 2): cd
SUBSTR('abcdefg', -3): efg
SUBSTR('abcdefg', 3, -2):
SUBSTR('abcdefg' from -4 for 2): de
1 row in set (0.001 sec)