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
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 .
In order to get the date from the timestamp, you can use DATE() function from MySQL.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With