Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select date only without the hour on postgresql [duplicate]

I have date column on my postgres table as timestamp format i.e "2017-01-01 22:00:00". When I wrote in the queries

select date from table where date = '2017-01-01' it did not give me any result.

Should I always include the time information on those queries? Can I just put the yyyy-mm-dd only on my queries to search the date column?

regards

like image 258
reyalino Avatar asked Feb 03 '17 07:02

reyalino


People also ask

How do I select just the date in PostgreSQL?

1) Get the current date However, to get the date part only (without the time part), you use the double colons (::) to cast a DATETIME value to a DATE value. The result is in the format: yyyy-mm-dd .

How do I get just the date from a timestamp?

In order to get the date from the timestamp, you can use DATE() function from MySQL.

How do I split a date and time in PostgreSQL?

This can be done in PostgreSQL using the AGE() function. This function takes in two timestamps as arguments and then returns the interval between them.


1 Answers

Cast to date:

select "date" from table where "date"::date = '2017-01-01'

Note that I enclosed references to the date column in double quotes, because date is a Postgres keyword. You should avoid naming your columns, tables, or schemas using keywords.

like image 151
Tim Biegeleisen Avatar answered Oct 06 '22 10:10

Tim Biegeleisen