Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Date to DateTime in PostgreSQL?

Tags:

postgresql

I'm trying to make the following work in PostgreSQL:

SELECT * FROM purchases WHERE CONVERT(expiration_date) < '2013-03-21 13:14:00'

Where CONVERT is some method that would convert Date to DateTime. Any idea how to do this?

like image 258
MikeMarsian Avatar asked Mar 19 '13 14:03

MikeMarsian


People also ask

How do I convert datetime to date in PostgreSQL?

PostgreSQL Convert DateTime to Date Using EXTRACT() Function EXTRACT() function takes in two arguments, field and source. The field argument specifies the date part, i.e. month, decade, hour, to extract from the DateTime value. The source argument is a value of type TIMESTAMP or INTERVAL.

How do I change the date format in PostgreSQL?

You can specify double colons (::) to cast a DATETIME value to a DATE value. You can combine TO_CHAR() and the Now() function to convert the current timestamp into the specified format.

How do I change the date format from YYYY-MM-DD in PostgreSQL?

How do I change the date format from YYYY-MM-DD in PostgreSQL? 1) Get the current date To get the current date and time, you use the built-in NOW() function. 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.

Does PostgreSQL have datetime?

PostgreSQL supports the full set of SQL date and time types, shown in Table 8.9. The operations available on these data types are described in Section 9.9.


1 Answers

There is no DateTime type in PostgreSQL, there's timestamp.

To convert simply do expiration_date::timestamp but I think it should work without conversion. WHERE expiration_date < '2013-03-21 13:14:00' should be valid.

like image 140
Jakub Kania Avatar answered Sep 21 '22 21:09

Jakub Kania