PostgreSQL DATE() Function
The PostgreSQL DATE() function is used to extract the date value from a date or datetime expression.
Syntax
DATE(expression)
Parameters
expression |
Required. Specify a valid date or datetime value from which the date should be extracted. Returns NULL if the expression is not a valid date or datetime value. |
Return Value
Returns the date value of a given date or datetime expression.
Example 1:
The example below shows the usage of DATE() function.
SELECT DATE('2018-08-18'); Result: '2018-08-18' SELECT DATE('2018-08-18 10:38:42'); Result: '2018-08-18' SELECT DATE('2018-08-18 10:38:42.000004'); Result: '2018-08-18' SELECT DATE('2014-10-25'); Result: '2014-10-25' SELECT DATE(CURDATE()); Result: '2021-11-16' SELECT DATE(NULL); Result: NULL SELECT DATE('Date is 2014-10-25'); Result: NULL
Example 2:
Consider a database table called Orders with the following records:
OrderQuantity | Price | OrderTime |
---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 |
120 | 1.61 | 2018-03-23 07:14:16 |
125 | 1.78 | 2018-09-12 05:25:56 |
50 | 1.80 | 2019-01-16 11:52:05 |
200 | 1.72 | 2020-02-06 09:31:34 |
The statement given below can be used to get the date value of records of column OrderTime:
SELECT *, DATE(OrderTime) AS DATE_Value FROM Orders;
This will produce the result as shown below:
OrderQuantity | Price | OrderTime | DATE_Value |
---|---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 | 2017-08-18 |
120 | 1.61 | 2018-03-23 07:14:16 | 2018-03-23 |
125 | 1.78 | 2018-09-12 05:25:56 | 2018-09-12 |
50 | 1.80 | 2019-01-16 11:52:05 | 2019-01-16 |
200 | 1.72 | 2020-02-06 09:31:34 | 2020-02-06 |
❮ PostgreSQL Functions