374

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 DATE value.

For example, if I have 2011/05/26 09:00:00, I want 2011/05/26

I tried casting, but I only get 2011:

timestamp:date
cast(timestamp as date)

I tried to_char() with to_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?

lospejos
  • 1,976
  • 3
  • 19
  • 35
keren
  • 3,807
  • 2
  • 15
  • 7

8 Answers8

609

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)
kworr
  • 3,579
  • 1
  • 21
  • 33
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
  • 3
    doesn't work, just tried "select '2010-01-01 12:00:00'::timestamp::date;" . it returns just year 2011. i had already tried date(timestamp) and (timestamp)::date but i only get the year part in return not the full date that i need. – keren May 26 '11 at 02:45
  • 2
    @kerenk, Now *that's* odd. Did you try it in psql? – Wayne Conrad May 26 '11 at 03:33
  • 44
    @keren, psql is a command-line utility--you're not using it (but do consider it). When you execute the query in pgadmin3, look at the data output pane. You can resize the columns; the default column size is too short to show the entire date and shows only the year. Use your mouse to expand that column and you should see the whole thing. – Wayne Conrad May 26 '11 at 03:40
  • 12
    omg you're right. i feel so stupid. thanks for pointing it out. – keren May 26 '11 at 03:45
  • One case I ran into where this doesn't work is in Squirrel. With this syntax, Squirrel will give you an input box to input parameter values for parameter ":date". – James Kingsbery Mar 26 '12 at 15:11
  • It is helpful.. I am also using *pgsql* right now.. It is hammering on my head. Let me try this solution... :) +1 in advance. – Arup Rakshit Jun 27 '14 at 09:39
  • Didn't work for me in Hibernate native queries. Solution (with the now() function): `CAST(NOW() AS date)` – Bevor Apr 06 '19 at 10:06
  • Beware of `::date` as it can get in conflict with parameters (`:myParameter`) in siome cases. – Zon Jul 08 '19 at 09:41
  • It works fine, i got a small error, but i was using a column alias like this, **column as c1::timestamp::date** , then i change it to **column::timestamp::date as c1** and it worked fine. – OJVM Jul 14 '21 at 21:13
147

Use the date function:

select date(timestamp_field) from table

From a character field representation to a date you can use:

select date(substring('2011/05/26 09:00:00' from 1 for 10));

Test code:

create table test_table (timestamp_field timestamp);
insert into test_table (timestamp_field) values(current_timestamp);
select timestamp_field, date(timestamp_field) from test_table;

Test result:

pgAdmin result

pgAdmin result wide

James Allman
  • 40,573
  • 11
  • 57
  • 70
22

In postgres simply :

TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date
RF1991
  • 2,037
  • 4
  • 8
  • 17
Elmira Behzad
  • 413
  • 4
  • 6
20

Have you tried to cast it to a date, with <mydatetime>::date ?

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • 1
    This works just fine. As noted in the comments on Wayne Conrad's answer, keren was led astray by an excessively narrow column in pgAdmin's output pane. – KenB Apr 24 '14 at 14:42
13

This works for me in python 2.7

 select some_date::DATE from some_table;
thedarkgriffen
  • 394
  • 3
  • 16
12

Just do select date(timestamp_column) and you would get the only the date part. Sometimes doing select timestamp_column::date may return date 00:00:00 where it doesn't remove the 00:00:00 part. But I have seen date(timestamp_column) to work perfectly in all the cases. Hope this helps.

Koushik Das
  • 9,678
  • 3
  • 51
  • 50
5
CREATE TABLE sometable (t TIMESTAMP, d DATE);
INSERT INTO sometable SELECT '2011/05/26 09:00:00';
UPDATE sometable SET d = t; -- OK
-- UPDATE sometable SET d = t::date; OK
-- UPDATE sometable SET d = CAST (t AS date); OK
-- UPDATE sometable SET d = date(t); OK
SELECT * FROM sometable ;
          t          |     d      
---------------------+------------
 2011-05-26 09:00:00 | 2011-05-26
(1 row)

Another test kit:

SELECT pg_catalog.date(t) FROM sometable;
    date    
------------
 2011-05-26
(1 row)

SHOW datestyle ;
 DateStyle 
-----------
 ISO, MDY
(1 row)
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
1

You can use date_trunc('day', field).

select date_trunc('day', data_gps) as date_description from some_table;
Ricardo Emerson
  • 856
  • 10
  • 11