| PostgreSQL | ||
|---|---|---|
| Prev | Next | |
多くのデータ型においては、関連する他の型への変換をするための関数が 用意されています。さらに、その型特有の関数を持つものもあります。 演算子を通しても使用できるような関数は、演算子としてのみ文書化 されています。
text型のための関数の中には、char() や varchar() においても使用できる ものがあります。
date_part 関数および date_trunc 関数においては、引数として、`year'、`month'、`day'、 `hour'、`minute'、`second' に加えて、より限定した量を表す `decade'、`century'、`millenium'、`millisecond'、`microsecond' も使えます。date_part 関数では、曜日を返す ための `dow' や、(datetime のための)1970 年からの 通算秒を返す `epoch' や、(timespan のための) 通算秒の合計を返す 'epoch' も使えます。
Table 10-1. 算術関数
| 関数 | 返り値 | 説明 | 例 |
|---|---|---|---|
| float(int) | float8 | 整数を浮動小数点数に変換 | float(2) |
| float4(int) | float4 | 整数を浮動小数点数に変換 | float4(2) |
| int | integer(float) | 浮動小数点数を整数に変換 | integer(2.0) |
多くの文字列関数は、text, varchar(), char() に対して使用できます。 現在のところ、 text 型にしか使えないものもあります。
Table 10-2. 文字列関数
| 関数 | 返り値 | 説明 | 例 |
|---|---|---|---|
| lower(text) | text | text を小文字に変換する | lower('TOM') |
| lpad(text,int,text) | text | 左側から指定の長さ分文字列で埋める | lpad('hi',4,'??') |
| ltrim(text,text) | text | text の左側から文字列を取り除く | ltrim('xxxxtrim','x') |
| position(text,text) | text | 指定した文字列を抽出する | position('high','ig') |
| rpad(text,int,text) | text | 右側から指定の長さ分文字列で埋める | rpad('hi',4,'x') |
| rtrim(text,text) | text | text の右側から文字列を取り除く t | rtrim('trimxxxx','x') |
| substr(text,int[,int]) | text | 指定した文字列を抽出する | substr('hi there',3,5) |
| upper(text) | text | text を大文字に変換する | upper('tom') |
Table 10-3. 日付/時刻関数
| 関数 | 返り値 | 説明 | 例 |
|---|---|---|---|
| isfinite(abstime) | bool | 有限時刻であれば真 | isfinite('now'::abstime) |
| datetime(abstime) | datetime | datetime へ変換 | datetime('now'::abstime) |
| datetime(date) | datetime | datetime へ変換 | datetime('today'::date) |
| datetime(date,time) | datetime | datetime へ変換 | datetime('1998-02-24'::datetime, '23:07'::time); |
| age(datetime,datetime) | timespan | 保持された年月の間の期間 | age('now','1957-06-13'::datetime) |
| date_part(text,datetime) | float8 | 日付フィールドの指定した部分 | date_part('dow','now'::datetime) |
| date_trunc(text,datetime) | datetime | 指定した単位で日付を切りつめる | date_trunc('month','now'::abstime) |
| isfinite(datetime) | bool | 有限時刻であれば真 | isfinite('now'::datetime) |
| abstime(datetime) | abstime | abstime へ変換 | abstime('now'::datetime) |
| timespan(reltime) | timespan | timespan へ変換 | timespan('4 hours'::reltime) |
| datetime(date,time) | datetime | datetime へ変換 | datetime('1998-02-25'::date,'06:41'::time) |
| date_part(text,timespan) | float8 | 時刻フィールドの指定した部分 | date_part('hour','4 hrs 3 mins'::timespan) |
| isfinite(timespan) | bool | 有限時刻であれば真 | isfinite('4 hrs'::timespan) |
| reltime(timespan) | reltime | reltime へ変換 | reltime('4 hrs'::timespan) |
Table 10-4. 座標関数
| 関数 | 返り値 | 説明 | 例 |
|---|---|---|---|
| box(point,point) | box | 座標点を長方形に変換する | box('(0,0)'::point,'(1,1)'::point) |
| area(box) | float8 | 長方形の面積 | area('((0,0),(1,1))'::box) |
| isopen(path) | bool | 開いているパスであれば真 | isopen('[(0,0),(1,1),(2,0)]'::path) |
| isclosed(path) | bool | 閉じたパスであれば真 | isclosed('((0,0),(1,1),(2,0))'::path) |
| circle(point,float8) | circle | 円に変換する | circle('(0,0)'::point,2.0) |
| polygon(npts,circle) | polygon | npts 個の座標点を持つ多角形に変換 | polygon(12,'((0,0),2.0)'::circle) |
| center(circle) | float8 | オブジェクトの中心点 | center('((0,0),2.0)'::circle) |
| radius(circle) | float8 | 円の半径 | radius('((0,0),2.0)'::circle) |
| diameter(circle) | float8 | 円の直径 | diameter('((0,0),2.0)'::circle) |
| area(circle) | float8 | 円の面積 | area('((0,0),2.0)'::circle) |
SQL92 では、関数を特有の書式で定義しています。これらのうちの いくつかは、他の Postgres 関数を 使って実装されています。
Table 10-5. SQL92 テキスト処理関数
| 関数 | 返り値 | 説明 | 例 |
|---|---|---|---|
| position(text in text) | int4 | 指定した文字列の位置を返す | position('o' in 'Tom') |
| substring(text [from int] [for int]) | text | 指定した文字列を抽出する | substring('Tom' from 2 for 2) |
| trim([leading|trailing|both] [text] from text) | text | text から文字列を取り除く | trim(both 'x' from 'xTomx') |
| Prev | Home | Next |
| 演算子 | Up | Arrays |