宣言
SUBSTRING(str, pos)
SUBSTRING(str, pos, len)
SUBSTRING(str FROM pos)
SUBSTRING(str FROM pos FOR len)
説明
str のサブストリングを返します。開始位置は pos、長さは len です。パラメータに NULL が含まれる場合、NULL を返します。この関数は SUBSTR() のエイリアスです。
lenを指定しない場合、返されるサブストリングはposの位置からstrの終端までの範囲となります。posの値が負の数の場合、strの末尾から頭部に向かって逆順にして開始位置を決定します。lenが0以下、またはposで指定された開始位置が無効な場合、空文字列を返します。
例
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