MySQL - CURRENT_DATE() Function



MySQL CURRENT_DATE() Function

The MySQL CURRENT_DATE() function is the synonym for CURDATE(). It is also used to retrieve the date of that day. The resultant value is a string or a numerical value based on the context and, the date returned will be in the 'YYYY-MM-DD' or YYYYMMDD format.

You can also use CURRENT_DATE instead of CURRENT_DATE() to retrieve the date of that day (present date). These are synonym functions.

Syntax

Following is the syntax of the above function −

CURRENT_DATE();

Parameters

This method does not accept any parameters.

Return value

This function returns the current date in the format 'YYYY-MM-DD'.

Example

Following example demonstrates the usage of the CURRENT_DATE() function −

SELECT CURRENT_DATE();

Output

Following output is obtained −

CURRENT_DATE()
2021-01-10

Example

Following is an example of this function in numerical context −

SELECT CURRENT_DATE() +0;

Output

Following output is obtained −

CURRENT_DATE() +0
20210110

Example

You can add days to the current date as shown below −

SELECT CURRENT_DATE()+12;

Output

Following output is obtained −

CURRENT_DATE()+12
20210122

Example

We can also subtract the desired number of days from the current date using this function −

SELECT CURRENT_DATE()-22213;

Output

Following output is obtained −

CURRENT_DATE()-22213
20,187,897

Example

You can use CURRENT_DATE instead of CURRENT_DATE() to retrieve the current date.

SELECT CURRENT_DATE;

Output

Following output is obtained −

CURRENT_DATE
2021-01-10

In Numeric Context

SELECT CURRENT_DATE+0;

Output

Following output is obtained −

CURRENT_DATE+0
20210110

Example

Let us create a table with name ORDERS in MySQL database using CREATE TABLE statement as shown below −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, we will insert records in ORDERS table using following INSERT statements −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

CURRENT_DATE()

Following query calculates the age of the players in days −

SELECT OID, DATE, DATEDIFF(CURRENT_DATE(), DATE), AMOUNT 
FROM ORDERS;

CURRENT_DATE

We can also retrieve the current date using CURRENT_DATE as shown in the query below −

SELECT OID, DATE, DATEDIFF(CURRENT_DATE, DATE), AMOUNT 
FROM ORDERS;

Output

The output is displayed as follows −

OID DATE DATEDIFF(CURRENT_DATE, DATE) AMOUNT
102 2009-10-08 00:00:00 5131 3000.00
100 2009-10-08 00:00:00 5131 1500.00
101 2009-11-20 00:00:00 5088 1560.00
103 2008-05-20 00:00:00 5637 2060.00
Advertisements