Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL

前端 未结 7 1477
南旧
南旧 2020-11-28 17:55

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 e

7条回答
  •  庸人自扰
    2020-11-28 18:32

    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)
    

提交回复
热议问题