MySQL - CURDATE() Function



The DATE, DATETIME and TIMESTAMP datatypes in MySQL are used to store the date, date and time, time stamp values respectively; where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.

MySQL CURDATE() Function

The MySQL CURDATE() function is used to get the current day's date. 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.

Syntax

Following is the syntax of MySQL CURDATE() function −

CURDATE();

Parameters

This method does not accept any parameters.

Return value

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

Example

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

SELECT CURDATE();

Output

Following output is obtained −

CURDATE()
2021-07-11

Example

Following is an example of this function in numerical context −

SELECT CURDATE() +0;

Output

Following output is obtained −

CURDATE() +0
20210711

Example

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

SELECT CURDATE()+12;

Output

Following output is obtained −

CURDATE()+12
20210723

Example

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

SELECT CURDATE()-22213;

Output

Following output is obtained −

CURDATE()-22213
20188498

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);

Following query calculates the difference between current date and ordered date (in days) −

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

Output

The output is displayed as follows −

OID DATE DATEDIFF(CURDATE(), 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