SUBSTRING

2024-06-28 05:30:31  Updated

Syntax

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

Purpose

SUBSTRING() 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. The function is the alias for SUBSTR().

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

Contact Us