3
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.

【PostgreSQL】文字列関数について

文字列関数とは

文字列関数とは文字列に対して処理を行った結果を返す関数。

関数 説明
lower(文字列) 指定した文字列を小文字に変換して返す
upper(文字列) 指定した文字列を大文字に変換して返す
char_length(文字列) 指定した文字列の文字数を返す
octet_length(文字列) 指定した文字列のバイト数を返す
trim(元文字列[, 除去する文字列]) 指定した元文字列から除去する文字列を取り除いて返す
除去する文字列を省略した場合は、デフォルトで空白文字が設定される
lpad(元文字列, 文字数[, 追加する文字列]) 指定した元文字列の先頭に、文字数に達するまで追加する文字列が埋め込まれる
追加する文字列を省略した場合は、デフォルトで空白文字が設定される
rpad(元文字列, 文字数[, 追加する文字列]) 指定した元文字列の末尾に、文字数に達するまで追加する文字列が埋め込まれる
追加する文字列を省略した場合は、デフォルトで空白文字が設定される
substring(文字列, 開始位置[, 文字数]) 指定した文字列の開始位置から、文字数分の文字列を取得して返す
文字数を省略した場合は、デフォルトで末尾までに設定される
replace(文字列, 置換前の文字列, 置換後の文字列) 指定した文字列中の置換前の文字列を、置換後の文字列に置き換えて返す

使い方

lower
postgres=# SELECT lower('TOM');
 lower 
-------
 tom
(1 )
upper
postgres=# SELECT upper('tom');
 upper 
-------
 TOM
(1 )
char_length
postgres=# SELECT char_length('postgresql');
 char_length 
-------------
          10
(1 )
octet_length
postgres=# SELECT octet_length('ab c');
 octet_length 
--------------
            4
(1 )

trim
postgres=# SELECT trim('yxTomxx', 'xyz');
 btrim 
-------
 Tom
(1 )
lpad
postgres=# SELECT lpad('hi', 5, 'xy');
 lpad  
-------
 xyxhi
(1 )
rpad
postgres=# SELECT rpad('hi', 5, 'xy');
 rpad  
-------
 hixyx
(1 )
substring
postgres=# SELECT substring('Thomas', 2, 3);
 substring 
-----------
 hom
(1 )

postgres=# SELECT substring('Thomas', 3);
 substring 
-----------
 omas
(1 )
replace
postgres=# SELECT replace('abcdefabcdef', 'cd', 'XX');
   replace    
--------------
 abXXefabXXef
(1 )
3
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
3
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?