PostgreSQLコマンドやSQL文に関する各種メモ書き

日付データをSELECT時に年・月・日に分解する

SELECT
date_part('year', now()) as 年
date_part('month', now()) as 月
date_part('day', now()) as 日

ただし 01月02日の場合、

月:1

日:2

になる(前にゼロがつかない)ので注意

ゼロをつけたい場合は

SELECT
to_char( now(), 'yyyy') as 年
to_char( now(), 'mm') as 月
to_char( now(), 'dd') as 日

とする

参考:http://winofsql.jp/VA003334/access050909140710.htm

No.437
07/29 13:59

edit