Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting date from timestamp in PostgreSQL

Tags:

I have a PostgreSQL timestamp as

2009-12-22 11:01:46 

I need to change this to date as

2009-12-22 

So that I can compare the dates in postgreSQL

How can I achieve this transformation?

like image 328
daydreamer Avatar asked Sep 06 '11 16:09

daydreamer


People also ask

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) .

How do I SELECT a date from a 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.)


1 Answers

Cast it to date.

SELECT yourtimestamp::date; 

If you need to extract other kinds of stuff, you might want to use EXTRACT or date_trunc

Both links are to the same page, were you'll find more date/time-related functions.

like image 60
plundra Avatar answered Oct 03 '22 10:10

plundra