Posted By: Anonymous
I want to extract just the date part from a timestamp in PostgreSQL.
I need it to be a postgresql
DATE type so I can insert it into another table that expects a
For example, if I have
2011/05/26 09:00:00, I want
I tried casting, but I only get 2011:
timestamp:date cast(timestamp as date)
SELECT to_date(to_char(timestamp, 'YYYY/MM/DD'), 'YYYY/MM/DD') FROM val3 WHERE id=1;
I tried to make it a function:
CREATE OR REPLACE FUNCTION testing() RETURNS void AS ' DECLARE i_date DATE; BEGIN SELECT to_date(to_char(val1, "YYYY/MM/DD"),"YYYY/MM/DD") INTO i_date FROM exampTable WHERE id=1; INSERT INTO foo(testd) VALUES (i); END
What is the best way to extract date (yyyy/mm/dd) from a timestamp in PostgreSQL?
You can cast your timestamp to a date by suffixing it with
::date. Here, in psql, is a timestamp:
# select '2010-01-01 12:00:00'::timestamp; timestamp --------------------- 2010-01-01 12:00:00
Now we’ll cast it to a date:
wconrad=# select '2010-01-01 12:00:00'::timestamp::date; date ------------ 2010-01-01
On the other hand you can use
date_trunc function. The difference between them is that the latter returns the same data type like
timestamptz keeping your time zone intact (if you need it).
=> select date_trunc('day', now()); date_trunc ------------------------ 2015-12-15 00:00:00+02 (1 row)