SUBSTR

2023-12-25 08:49:42  Updated

Syntax

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

Purpose

SUBSTR() returns a substring of str. The start position is pos, and the length of the substring is len characters. If any argument is NULL, the function returns NULL.

  • If len is not specified, the returned substring starts from the position pos till the end of str.

  • If the value of pos is negative, the start position is reversely determined from the end of str.

  • If len is less than or equal to 0, or the start position specified by pos is invalid, an empty string is returned.

Examples

obclient> 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

Contact Us