2011/08/08

to_char による日付の翻訳 (Multiple language support with to_char)

データの翻訳について、今回はBI Publisherではなく、Oracle DBに実装されているto_charについて日付→文字列変換の例を示します。

to_charの出力結果はnls_date_languageの設定により変化します。システムの環境に準じる場合には以下の構文を使用します。
select to_char(sysdate, 'yyyy"年" month dd"日" day') as col1  from dual;

to_charの実行時にnls_date_languageを指定することもできます。
select to_char(sysdate, 'yyyy month dd day', 'nls_date_language=''american'' ') as col1 from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_date_language=''japanese'' ') as col1  from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_date_language=''hindi'' ') as col1  from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_date_language=''arabic'' ') as col1  from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_date_language=''spanish'' ') as col1  from dual

※DBのキャラクタセットがunicodeでない場合はto_ncharを使用します。
実行結果は以下の通りです。
Figure 1: nls_date_language



また、和暦その他の暦年を使用する場合にはnls_calendarを設定します。この際、nls_date_languageも併せて指定しないとバッファあふれのエラー(ORA-01801)が発生する場合がありますので注意してください。
和暦の場合は元号を表すee(またはe)を書式に記載します。
select to_char(sysdate, 'yy"年" month dd"日" day', 'nls_calendar=''japanese imperial'' nls_date_language=''japanese'' ') as col1  from dual
union all
select to_char(sysdate, 'eeyy"年" month dd"日" day', 'nls_calendar=''japanese imperial'' nls_date_language=''japanese'' ') as col1  from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_calendar=''arabic hijrah'' nls_date_language=''arabic'' ') as col1  from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_calendar=''english hijrah'' nls_date_language=''american'' ') as col1  from dual
union all
select to_char(sysdate, 'yyyy month dd day', 'nls_calendar=''thai buddha'' nls_date_language=''thai'' ') as col1 from dual

実行結果は以下の通りです。
Figure 2: nls_calendar



【参考】
OTNのスレッドが参考になります。
http://forums.oracle.com/forums/thread.jspa?threadID=180322


[Summary]
The third parameter of to_char manages NLS.  You can translate date value with SQL.  Sample scripts are shown above.
Note that you should not use nls_calendar alone.  To avoid ORA-01801, nls_date_language should accompany with the calendar setting.


OTN:
http://forums.oracle.com/forums/thread.jspa?threadID=180322

0 件のコメント:

コメントを投稿