Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 528
keren Avatar asked May 26 '11 02:05

keren


People also ask

Can we extract date from timestamp in SQL?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column. (In our example, we use a column of the timestamp data type.)

How do I convert datetime to date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .

What is the format of timestamp in PostgreSQL?

Postgres DATE data type Postgres uses the DATE data type for storing different dates in YYYY-MM-DD format. It uses 4 bytes for storing a date value in a column. You can design a Postgres table with a DATE column and use the keyword DEFAULT CURRENT_DATE to use the current system date as the default value in this column.


2 Answers

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) 
like image 81
Wayne Conrad Avatar answered Oct 07 '22 01:10

Wayne Conrad


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

like image 41
James Allman Avatar answered Oct 07 '22 02:10

James Allman