polar_utility プラグインは、PolarDB for Oracle に追加の間隔関数とデータ変換関数を拡張します。各データベースに一度インストールすると、その関数を SQL で直接呼び出すことができます。
プラグインのインストールと削除
プラグインをインストールします。
CREATE EXTENSION polar_utility;プラグインを削除します。
DROP EXTENSION polar_utility;関数リファレンス
polar_utility は、2 つの関数グループを提供します。
間隔関数 — interval 値を数値の日数に変換し、変換を適用します。
| 関数 | 戻り値 | 説明 | クイック例 |
|---|---|---|---|
to_number(interval) | numeric | 日数(10 進数) | to_number(interval '2.1 day') → 2.1 |
trunc(interval) | numeric | 期間の日数、整数部のみ | trunc(interval '2.1 day') → 2 |
abs(interval) | numeric | 絶対日数 | abs(interval '-1 day') → 1 |
to_char(interval) | text | 文字列としての期間の日数 | to_char(interval '2.1 day') → '2.1' |
変換関数 — 値を解析または再エンコードします。
| 関数 | 戻り値 | 説明 | クイック例 |
|---|---|---|---|
to_date(numeric, character varying) | date | 指定されたフォーマットマスクを使用して、数値日付文字列を解析します。 | to_date('20200811','yyyymmddhh24miss') → Tue Aug 11 00:00:00 2020 |
to_date(timestamp without time zone) | date | タイムスタンプ値を日付値にキャストします。 | to_date('11-AUG-20 03:16:09.089308'::timestamp) → Tue Aug 11 03:16:09.089308 2020 |
mod(text, text) | numeric | 2 つの text 値に対して剰余演算を実行します。計算の前に値は numeric にキャストされます。 | mod('10'::text, '2'::text) → 0 |
convert(text, text) | text | 文字列を 2 番目の引数で指定されたターゲットエンコーディングに再エンコードします。 | convert('RAM is the new disk.', 'utf8') → 'RAM is the new disk.' |
テストデータのセットアップ
以下の例は共通のテストテーブルを共有しています。以下のステートメントを一度実行して作成します。
CREATE TABLE polar_test_interval_func(a interval);
INSERT INTO polar_test_interval_func
VALUES (interval '1.1 hour'),
(interval '2.1 day'),
(interval '3.1 month'),
(interval '4.3 year'),
(('11-AUG-20 03:16:09.089308'::timestamp - 1) - '11-AUG-20 03:16:09.089308'::timestamp),
(('11-AUG-20 03:16:09.089308'::timestamp + 3) - '11-AUG-20 03:16:09.089308'::timestamp);間隔関数
to_number(interval)
interval 値を等価な日数に変換し、numeric として返します。
SELECT to_number(a) FROM polar_test_interval_func;Result:
to_number
-------------------------
0.04583333333333333333
2.1000000000000000
93.0000000000000000
1551.0000000000000000
-1.00000000000000000000
3.0000000000000000
(6 rows)trunc(interval)
interval 値を等価な日数に変換し、小数部を削除して、整数部を numeric として返します。
SELECT trunc(a) FROM polar_test_interval_func;Result:
trunc
-------
0
2
93
1551
-1
3
(6 rows)abs(interval)
interval 値を等価な日数に変換し、その絶対値を numeric として返します。
SELECT abs(a) FROM polar_test_interval_func;Result:
abs
------------------------
0.04583333333333333333
2.1000000000000000
93.0000000000000000
1551.0000000000000000
1.00000000000000000000
3.0000000000000000
(6 rows)to_char(interval)
interval 値を等価な日数に変換し、その日数を text 文字列として返します。
SELECT to_char(a) FROM polar_test_interval_func;Result:
to_char
-------------------------
0.04583333333333333333
2.1000000000000000
93.0000000000000000
1551.0000000000000000
-1.00000000000000000000
3.0000000000000000
(6 rows)変換関数
to_date(numeric, character varying)
指定されたフォーマットマスクを使用して、数値日付文字列を日付値に変換します。
SELECT to_date('20200811','yyyymmddhh24miss');Result:
to_date
--------------------------
Tue Aug 11 00:00:00 2020
(1 row)to_date(timestamp without time zone)
timestamp 値を受け入れ、日付値を返します。
SELECT to_date('11-AUG-20 03:16:09.089308'::timestamp);Result:
to_date
---------------------------------
Tue Aug 11 03:16:09.089308 2020
(1 row)mod(text, text)
2 つの text 値に対して剰余演算を実行します。計算の前に値は numeric にキャストされます。
SELECT mod('10'::text, '2'::text);Result:
mod
-----
0
(1 row)convert(text, text)
文字列を 2 番目の引数で指定されたターゲットエンコーディングに再エンコードします。
SELECT convert('RAM is the new disk.', 'utf8');Result:
convert
----------------------
RAM is the new disk.
(1 row)