TO_CHAR - Amazon Redshift

TO_CHAR

TO_CHAR は、タイムスタンプまたは数値式を文字列データ形式に変換します。

構文

TO_CHAR (timestamp_expression | numeric_expression , 'format')

引数

timestamp_expression

TIMESTAMP 型または TIMESTAMPTZ 型の値、またはタイムスタンプに暗黙的に強制変換できる値を生成する式。

numeric_expression

数値データ型の値、または数値型に暗黙的に強制変換できる値が生成される式。詳細については、「数値型」を参照してください。TO_CHAR は、数文字列の左側にスペースを挿入します。

注記

TO_CHAR は、128 ビットの DECIMAL 値をサポートしません。

format

新しい値の形式。有効な形式については、「日時形式の文字列」および「 数値形式の文字列」を参照してください。

戻り型

VARCHAR

次の例では、月の名前を 9 文字に埋め込み、曜日の名前と日付の数字を指定した形式でタイムスタンプを日付と時刻の値に変換します。

select to_char(timestamp '2009-12-31 23:15:59', 'MONTH-DY-DD-YYYY HH12:MIPM'); to_char ------------------------- DECEMBER -THU-31-2009 11:15PM

次の例では、タイムスタンプを日付の番号の値に変換します。

select to_char(timestamp '2009-12-31 23:15:59', 'DDD'); to_char ------------------------- 365

次の例では、タイムスタンプを曜日の番号の値に変換します。

select to_char(timestamp '2022-05-16 23:15:59', 'ID'); to_char ------------------------- 1

以下の例では、日付から月を抽出しています。

select to_char(date '2009-12-31', 'MONTH'); to_char ------------------------- DECEMBER

次の例は、EVENT テーブル内の各 STARTTIME 値を、時、分、および秒から成る文字列に変換します。

select to_char(starttime, 'HH12:MI:SS') from event where eventid between 1 and 5 order by eventid; to_char ---------- 02:30:00 08:00:00 02:30:00 02:30:00 07:00:00

次の例は、タイムスタンプの値全体を別の形式に変換します。

select starttime, to_char(starttime, 'MON-DD-YYYY HH12:MIPM') from event where eventid=1; starttime | to_char ---------------------+--------------------- 2008-01-25 14:30:00 | JAN-25-2008 02:30PM

次の例は、タイムスタンプリテラルをキャラクタ文字列に変換します。

select to_char(timestamp '2009-12-31 23:15:59','HH24:MI:SS'); to_char ---------- 23:15:59

次の例では、10 進数をキャラクタ文字列に変換します。

select to_char(125.8, '999.99'); to_char --------- 125.80

次の例では、10 進数をキャラクタ文字列に変換します。

select to_char(125.8, '999D99'); to_char --------- 125.80

次の例では、先頭にゼロを含む数字をキャラクタ文字列に変換します。

select to_char(125.8, '0999D99'); to_char --------- 0125.80

次の例では、数字を負の記号を末尾に付けた文字列に変換します。

select to_char(-125.8, '999D99S'); to_char --------- 125.80-

次の例では、指定された位置に正または負の記号を末尾に付けた文字列に数字を変換します。

select to_char(125.8, '999D99SG'); to_char --------- 125.80+

次の例では、指定された位置に正の記号を末尾に付けた文字列に数字を変換します。

select to_char(125.8, 'PL999D99'); to_char --------- + 125.80

次の例では、数字を通貨記号付きの文字列に変換します。

select to_char(-125.88, '$S999D99'); to_char --------- $-125.88

次の例では、指定された位置に通貨記号を付けた文字列に数字を変換します。

select to_char(-125.88, 'S999D99L'); to_char --------- -125.88$

次の例では、3 桁の区切り文字 (コンマ) を使用して数字をキャラクタ文字列に変換します。

select to_char(1125.8, '9,999.99'); to_char --------- 1,125.80

次の例では、負の数字に角括弧を使用して、数字を文字列に変換します。

select to_char(-125.88, '$999D99PR'); to_char --------- $<125.88>

次の例では、数字をローマ字の文字列に変換します。

select to_char(125, 'RN'); to_char --------- CXXV

次の例では、日付を世紀コードに変換します。

select to_char(date '2020-12-31', 'CC'); to_char --------- 21

次の例では、曜日を表示します。

SELECT to_char(current_timestamp, 'FMDay, FMDD HH12:MI:SS'); to_char ----------------------- Wednesday, 31 09:34:26

次の例では、数に対して序数のサフィックスを表示します。

SELECT to_char(482, '999th'); to_char ----------------------- 482nd

次の例では、販売テーブル内の支払い価格からコミッションを減算します。誤差は四捨五入されて、ローマ数字に変換され、to_char 列に表示されます。

select salesid, pricepaid, commission, (pricepaid - commission) as difference, to_char(pricepaid - commission, 'rn') from sales group by sales.pricepaid, sales.commission, salesid order by salesid limit 10; salesid | pricepaid | commission | difference | to_char ---------+-----------+------------+------------+----------------- 1 | 728.00 | 109.20 | 618.80 | dcxix 2 | 76.00 | 11.40 | 64.60 | lxv 3 | 350.00 | 52.50 | 297.50 | ccxcviii 4 | 175.00 | 26.25 | 148.75 | cxlix 5 | 154.00 | 23.10 | 130.90 | cxxxi 6 | 394.00 | 59.10 | 334.90 | cccxxxv 7 | 788.00 | 118.20 | 669.80 | dclxx 8 | 197.00 | 29.55 | 167.45 | clxvii 9 | 591.00 | 88.65 | 502.35 | dii 10 | 65.00 | 9.75 | 55.25 | lv

次の例では、さまざまな値に通貨記号を追加して、to_char 列に表示します。

select salesid, pricepaid, commission, (pricepaid - commission) as difference, to_char(pricepaid - commission, 'l99999D99') from sales group by sales.pricepaid, sales.commission, salesid order by salesid limit 10; salesid | pricepaid | commission | difference | to_char --------+-----------+------------+------------+------------ 1 | 728.00 | 109.20 | 618.80 | $ 618.80 2 | 76.00 | 11.40 | 64.60 | $ 64.60 3 | 350.00 | 52.50 | 297.50 | $ 297.50 4 | 175.00 | 26.25 | 148.75 | $ 148.75 5 | 154.00 | 23.10 | 130.90 | $ 130.90 6 | 394.00 | 59.10 | 334.90 | $ 334.90 7 | 788.00 | 118.20 | 669.80 | $ 669.80 8 | 197.00 | 29.55 | 167.45 | $ 167.45 9 | 591.00 | 88.65 | 502.35 | $ 502.35 10 | 65.00 | 9.75 | 55.25 | $ 55.25

次の例では、各販売が行われた世紀を一覧で示します。

select salesid, saletime, to_char(saletime, 'cc') from sales order by salesid limit 10; salesid | saletime | to_char ---------+---------------------+--------- 1 | 2008-02-18 02:36:48 | 21 2 | 2008-06-06 05:00:16 | 21 3 | 2008-06-06 08:26:17 | 21 4 | 2008-06-09 08:38:52 | 21 5 | 2008-08-31 09:17:02 | 21 6 | 2008-07-16 11:59:24 | 21 7 | 2008-06-26 12:56:06 | 21 8 | 2008-07-10 02:12:36 | 21 9 | 2008-07-22 02:23:17 | 21 10 | 2008-08-06 02:51:55 | 21

次の例は、EVENT テーブル内の各 STARTTIME 値を、時、分、秒、およびタイムゾーンから成る文字列に変換します。

select to_char(starttime, 'HH12:MI:SS TZ') from event where eventid between 1 and 5 order by eventid; to_char ---------- 02:30:00 UTC 08:00:00 UTC 02:30:00 UTC 02:30:00 UTC 07:00:00 UTC

以下の例では、秒、ミリ秒、マイクロ秒の形式を示しています。

select sysdate, to_char(sysdate, 'HH24:MI:SS') as seconds, to_char(sysdate, 'HH24:MI:SS.MS') as milliseconds, to_char(sysdate, 'HH24:MI:SS:US') as microseconds; timestamp | seconds | milliseconds | microseconds --------------------+----------+--------------+---------------- 2015-04-10 18:45:09 | 18:45:09 | 18:45:09.325 | 18:45:09:325143