0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Oracleで文字列を『左端〇文字目から〇文字分切り出し』と『右端から切り出し』

Last updated at Posted at 2022-08-08

LEFT()やRIGHT()は存在しない

まず前提としてOracleには LEFT() RIGT() という関数は無いです。
その代わりとして SUBSTR() があります。

左から切り出し例

第二引数で 切り出し開始位置
第三引数で 何文字分切り出すか
第三引数を省略した場合は 最後まで切り出し になる。

SELECT 

-- 左端から1文字切り出し。結果は『あ』
SUBSTR('あいうえお',1,1) AS test1, 

-- 左から2文字目から1文字分切り出し。結果は『い』
SUBSTR('あいうえお',2,1) AS test2, 

-- 存在しない位置から切り出し。例外にならずに結果はNULL。
SUBSTR('あ',10,1) AS test3, 

-- NULLから切り出し。例外にならずに結果はNULL。
SUBSTR(NULL,2,1) AS test4

-- 左から二文字目以降を全て切り出し。結果は『いうえお』。
SUBSTR('あいうえお',2) AS test5

FROM テーブル名

右端からの切り出し例

第二引数で 何文字分切り出すか

SELECT 

-- 右端から1文字切り出し。結果は『お』
SUBSTR('あいうえお',-1) AS test1, 

-- 文字数を超える切り出し。例外にならずに結果はNULL。
SUBSTR('あ',-10) AS test2, 

-- NULLから切り出し。例外にならずに結果はNULL。
SUBSTR(NULL,-1) AS test3

FROM テーブル名

参考サイトさん

バージョン

Oracle Database 11g Release 11.2.0.1.0 - 64bit Production

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?