メインコンテンツまでスキップ
バージョン: V1.1.0

SUBSTRING

宣言​

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 で指定された開始位置が無効な場合、空文字列を返します。

例​

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)