2015年4月23日木曜日

PostgreSQLで 年月日のみの日付型データ取得方法

-- PostgreSQLで 年月日のみの日付型データ取得方法
-- 整数秒(timestamp(0))で現在時刻取得、"日"より下を切り捨てる
select date_trunc('day',cast(current_timestamp as timestamp(0)))
union all
-- ミリ秒で現在時刻取得、"日"より下を切り捨てる
select date_trunc('day', current_timestamp)
union all
--日付/時刻関数「current_date」を用いる
select current_date;

参考URL

[整数秒:timestamp(0]
タイムスタンプでマイクロ秒ではなく整数秒を利用する http://www.ecoop.net/memo/archives/2006-08-10-2.html
[日付切り捨て:date_trunc]
http://homepage2.nifty.com/sak/w_sak3/doc/sysbrd/psql_k14.htm
[日付/時刻関数:current_date]
PostgreSQL 7.2.3 ユーザガイド:4.8. 日付/時刻関数と演算子
https://www.postgresql.jp/document/7.2/user/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT
date_trunc('day', now())の落とし穴
http://qiita.com/shimomai@github/items/03d114358a5d455d4510