Skip to main content
Version: V1.0.0

SUBSTRING

Declaration

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

Description

Returns a substring of str starting at position pos and having a length of len. If any parameter is NULL, the function returns NULL. This function is an alias for SUBSTR().

  • If len is not specified, the substring starts at pos and extends 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, an empty string is returned.

Examples

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