tags:

views:

327

answers:

3

How to write a postgresql query for getting only the date part of timestamp field, from a table

+3  A: 
select date(my_field) from my_table;
Konrad Garus
thank you very mach, its simple and easy one
Linto davis
A: 

Another option would be to cast your timestamp to a date:

SELECT

CAST('2010-01-01 12:12:12' AS date)

Frank Heikens
+1  A: 

You two basic options, each with a number of equivalent expressions. Assuming a TIMESTAMP field named "ts":

  • By type cast
    • CAST(ts AS DATE) SQL-compliant syntax
    • ts::DATE Historical pg syntax
    • DATE(ts) Actually a function. Note that this syntax is deprecated, per the link above.
  • By date/time function
    • EXTRACT(YEAR FROM ts)
    • DATE_PART('YEAR', ts)
pilcrow