#navi(../)
* 日時情報からYYYYMMDDを取り出す方法 [#xca8fb18]
to_char関数を使い、timestamp型に格納された値から日付を取り出し、出力フォーマットを加工する方法を以下に記します。~
本資料は、参考資料に記した[[PostgreSQLオンラインマニュアル>http://www.postgresql.jp/document/]]の一部を引用および利用しています。

#contents
#htmlinsertpcsp(db-top.html,db-sp.html)

* 参考資料 [#c1f98f6f]
-PostgreSQLのオンラインマニュアル~
http://www.postgresql.jp/document/9.1/html/functions-formatting.html

* to_char関数を利用した例 [#c3c3ea01]
以下にto_char関数について記します。

** to_char関数の書式 [#ha104696]
to_char関数の書式は以下のようになります。
 to_char(timestamp, text)
textの部分が出力書式となります。

** 出力書式 [#zd28012c]
この資料では、実際に使用すると思われる書式のみ抜粋しています。~
詳細は、上記参考資料の[[PostgreSQLオンラインマニュアル>http://www.postgresql.jp/document/]]で確認してみてください。~
尚、以下のパターンは英大文字ですが、英小文字でも同様の動作をします。

|''パターン''|''出力''|
|''YYYY''|''年(4桁)''|
|''YY''|''年(下2桁)''|
|''MM''|''月(01-12)''|
|''DD''|''日(01-31)''|
|HH|時(01-12)|
|HH12|時(01-12)|
|''HH24''|''時(01-23)''|
|''MI''|''分(00-59)''|
|''SS''|''秒(00-59)''|
|MS|秒(000-999)|

* to_char関数の実行例 [#p4c11831]
以下のようにtimestampデータをinsertしてからto_char関数を確かめています。~
#ref(sample.sql)
 create table t1 (
   c1 timestamp
 );
 
 insert into t1 values
 ('2012-01-10 01:01:01'),
 ('2012-02-20 03:10:11'),
 ('2012-03-30 05:20:22'),
 ('2012-04-04 07:30:33'),
 ('2012-05-05 09:40:44'),
 ('2012-06-06 11:50:55'),
 ('2012-07-11 13:59:44'),
 ('2012-08-21 15:40:33'),
 ('2012-09-30 17:30:22'),
 ('2012-10-10 19:20:11'),
 ('2012-11-20 21:10:00'),
 ('2012-12-31 23:00:01');

上記SQLを実行した後の状態です。
 sakura=# \d t1
              テーブル "public.t1"
  カラム |             型              | 修飾語 
 --------+-----------------------------+--------
  c1     | timestamp without time zone | 
 
 sakura=# select * from t1;
          c1          
 ---------------------
  2012-01-10 01:01:01
  2012-02-20 03:10:11
  2012-03-30 05:20:22
  2012-04-04 07:30:33
  2012-05-05 09:40:44
  2012-06-06 11:50:55
  2012-07-11 13:59:44
  2012-08-21 15:40:33
  2012-09-30 17:30:22
  2012-10-10 19:20:11
  2012-11-20 21:10:00
  2012-12-31 23:00:01
 (12 行)

** to_charでyyyy/mm/dd hh24:mi:ss を実行してみる [#z733a587]
上記SQLで準備したデータを使用しyyyy-mm-dd hh24:mi:ssを試してみます。
 sakura=# select to_char(c1, 'yyyy/mm/dd hh24:mm:ss') from t1;
        to_char       
 ---------------------
  2012/01/10 01:01:01
  2012/02/20 03:02:11
  2012/03/30 05:03:22
  2012/04/04 07:04:33
  2012/05/05 09:05:44
  2012/06/06 11:06:55
  2012/07/11 13:07:44
  2012/08/21 15:08:33
  2012/09/30 17:09:22
  2012/10/10 19:10:11
  2012/11/20 21:11:00
  2012/12/31 23:12:01
 (12 行)

** 変換した後のカラム名がto_charとなっているのを修正する [#r1155800]
as を利用して表示をc1としました。
 sakura=# select to_char(c1, 'yyyy/mm/dd hh24:mm:ss') as c1 from t1;
          c1          
 ---------------------
  2012/01/10 01:01:01
  2012/02/20 03:02:11
  2012/03/30 05:03:22
  2012/04/04 07:04:33
  2012/05/05 09:05:44
  2012/06/06 11:06:55
  2012/07/11 13:07:44
  2012/08/21 15:08:33
  2012/09/30 17:09:22
  2012/10/10 19:10:11
  2012/11/20 21:11:00
  2012/12/31 23:12:01
 (12 行)

** to_charでyyyy/mm/dd を実行してみる [#na0a8b4f]
 sakura=# select to_char(c1, 'yyyy/mm/dd') as c1 from t1;
      c1     
 ------------
  2012/01/10
  2012/02/20
  2012/03/30
  2012/04/04
  2012/05/05
  2012/06/06
  2012/07/11
  2012/08/21
  2012/09/30
  2012/10/10
  2012/11/20
  2012/12/31
 (12 行)

#htmlinsertpcsp(db-btm.html,db-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS