ファンクションの移行
構文や機能性の違いの他に注意するのが、SELECT文などで組み込みファンクションを使用している場合です。
「等価な機能を持った組み込みファンクションが移行先にない」あるいは「等価なものがあるが名前が違う」などの場合には変更する必要があります。
ファンクション対応表
Oracleの主な関数に対応する「PostgreSQLファンクション対応表」を表5に示します。各関数の引数の意味や順番は異なることがありますので、移行の際にはそれぞれのマニュアルを参照してください。
表5を見ると、Oracleの組み込みファンクションに対して、意外に多くのPostgreSQLファンクションが対応していることがわかります。 OracleにあってPostgreSQLに存在しないのは、XMLを処理するファンクションや、さまざまな集計処理を行う分析ファンクションなどです。 そのPostgreSQLファンクション対応表を次表に示します。
単一行ファンクション
| Oracle | PostgreSQL |
| ABS | abs(x) |
| ACOS | acos(x) |
| ASIN | asin(x) |
| ATAN | atan(x) |
| ATAN2 | atan2(x, y) |
| BITAND | bitand(x, y) |
| CEIL | ceil(dp or numeric) |
| COS | cos(x) |
| EXP | exp(dp or numeric) |
| FLOOR | floor(dp or numeric) |
| LN | ln(dp or numeric) |
| LOG | log(dp or numeric) |
| MOD | mod(y, x) |
| POWER | pow(a dp, b dp) |
| ROUND | (数値) round(dp or numeric) |
| SIGN | sign(dp or numeric) |
| SIN | sin(x) |
| SQRT | sqrt(dp or numeric) |
| TAN | tan(x) |
| TRUNC | (数値) trunc(dp or numeric) |
表5-1:数値ファンクション
| Oracle | PostgreSQL |
| CHR | chr(integer) |
| CONCAT | concat(string1, string2) |
| INITCAP | initcap(text) |
| LOWER | lower(string) |
| LPAD | lpad(string text, length integer[, fill text]) |
| LTRIM | ltrim(string text, characters text) |
| REPLACE | replace(string text, from text, to text) |
| RPAD | rpad(string text, length integer [, fill text]) |
| RTRIM | rtrim(string text, characters text) |
| SUBSTR | substr(string, from [, count]) |
| TRANSLATE | translate(string text, from text, to text) |
| TRIM | trim([leading / trailing / both][characters] from string) |
| UPPER | upper(string) |
表5-2:文字値を戻す文字ファンクション
| Oracle | PostgreSQL |
| ASCII | ascii(text) |
| INSTR | instre(string1, string2) |
| LENGTH | length(string) |
| LENGTHB | octet_length(string) |
表5-3:数値を戻す文字ファンクション
| Oracle | PostgreSQL |
| ADD_MONTHS | add_months(date, add_number) |
| CURRENT_DATE | current_date |
| CURRENT_TIMESTAMP | current_timestamp |
| EXTRACT(日時) | extract(field from timestamp) |
| LAST_DAY | last_day(date) |
| LOCALTIMESTAMP | localtimestamp |
| MONTHS_BETWEEN | months_between(date1, date2) |
| NEXT_DAY | next_day(date, string) |
| SYSDATE | current_date |
| SYSTIMESTAMP | systimestamp |
| TO_TIMESTAMP | to_timestamp(text, text) |
| TRUNC(日付) | date_trunc(text, timestamp) |
表5-4:日時ファンクション
| Oracle | PostgreSQL |
| ASCIISTR | to_ascii(text [, encoding]) |
| BIN_TO_NUM | bin_to_num(int[, ...]) |
| CAST | CAST(expression AS type) |
| CONVERT | convert(string text,[src_encoding name,]dest_encoding name) |
| TO_CHAR(日時) | to_char(timestamp, text) |
| TO_CHAR(数値) | to_char(int, text) |
| TO_DATE | to_date(text, text) |
| TO_NUMBER | to_number(text, text) |
表5-5:変換ファンクション
| Oracle | PostgreSQL |
| COALESCE | COALESCE(value [, ...]) |
| DECODE | decade(string text, type text) |
| GREATEST | greatest(value [, ...]) |
| LEAST | least(value [, ...]) |
| NULLIF | NULLIF(value1, value2) |
| NVL | COALESCE(value [, ...]) |
| NVL2 | COALESCE(value [, ...]) |
| USER | user |
表5-6:その他の単一行ファンクション
集計ファンクション
| Oracle | PostgreSQL |
| AVG | avg(expression) |
| COUNT | count(expression) |
| MAX | max(expression) |
| MIN | min(expression) |
| STDDEV | stddev(expression) |
| SUM | sum(expression) |
| VARIANCE | variance(expression) |
表5-7:集計ファンクション
組み込みファンクションは、システムに埋め込まれたSQL文の中で、数多く使用されていることが考えら れます。これらをすべて変更するのは大変ですが、構文と違ってファンクションはユーザが簡単に定義できます。そのため、同名・同機能の関数を作成してしま えば、移行の手間が減ってラクになります。同じ機能で名前だけが違う関数があるなら、同名の関数を作って、その中から呼び出すようにするのがよいでしょ う。できるだけ変更の手間を減らすのが移行のコツです。
次からは、変換に注意を要するものを見ていきましょう。