EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL CURDATE

MySQL CURDATE

Payal Udhani
Article byPayal Udhani
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated May 24, 2023

MySQL CURDATE

Introduction to MySQL CURDATE

Manier times while inserting the values in MySQL tables with date columns, we have to insert the date column that is the date at that particular time of insertion, or even set the default value of the date column to the date on which that record is inserted. In all such cases, we need to retrieve the value of the date at that particular instance when the operation is being performed. For this, Mysql has provided the functionality to use the CURDATE function to retrieve the current date.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

In this article, we will learn how to use the CURDATE function, its syntax and example, alternatives of the CURDATE function in MySQL, and the difference between the NOW() functionality and the CURDATE() functionality.

Syntax

string/number CURDATE();

Explanation: The CURDATE() function returns the value of the current date or the date at that particular instance when the function is being called. The date format depends upon the context in which the function is being called. For example, when string context is used, the retrieved data’s format is in the ‘YYYY-MM-DD’ format. At the same time, in the case of numeric context, the format of the fetched date changes to YYYY-MM-DD format.

How does MySQL CURDATE work?

Let us retrieve the value of the CURDATE() function using the simple SELECT statement as follows:

Code:

SELECT CURDATE();

Output:

mysql curdate1

Explanation: that is the date when the article is being written in the ‘YYYY-MM-DD’ format. As we can see, the default format used is the ‘YYYY-MM-DD’, and the return type is a string. Let us convert the retrieved value in the numeric context value by adding 0 or 1 value or some other number to convert the returned value implicitly to the numeric value. We will use the following query statements.

Code:

SELECT 0 + CURDATE();

Output:

mysql curdate2

Explanation: Execution of the above query statement gives the following output with today’s date value in YYYY-MM-DD format.

Code:

SELECT 1 + CURDATE();

Output:

mysql curdate3

Explanation: Execution of the above query statement gives the following output with tomorrow’s date value as it is equivalent to today’s date plus one day in YYYY-MM-DD format.

Examples to Implement MySQL CURDATE

Below are some examples mentioned:

Example #1

Let us see how we can use it in the table to insert the date column to today’s date. We will create a table named educba_writers with a joining_date column with date data type with the help of the following query statement:

Code:

CREATE TABLE 'educba_writers' (
'id' int(11) NOT NULL,
'firstName' varchar(10) COLLATE latin1_danish_ci NOT NULL,
'rate' decimal(5,2) DEFAULT NULL,
'joining_date' date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_danish_ci;

Output:

mysql curdate4

Example #2

We will insert the values in the educba_writers table with today’s date as the joining_date value using the CURDATE function. For this, we will use the following insert query statement. Let us insert some more rows with non-null rate and joining date value:

Code:

INSERT INTO 'educba_writers' ('id', 'firstName', 'rate', 'joining_date') VALUES
(1, 'Payal', 750, CURDATE());

Output:

mysql curdate5

Example #3

Let us retrieve the record and view the inserted value in the educba_writers table using the following query statement:

Code:

SELECT * FROM educba_writers;

Output:

retrieve the record

Example #4

We can see that the value of today’s date, 2020-05-27, in the ‘YYYY-MM-DD’ format is inserted in the joining_date column. We can even insert the dates concerning today’s date using the CURDATE() function. Like for example, we can add the joining date as two days prior or three days later than today by reducing that many days from the CURDATE() function. Suppose we have to insert a writer record named Vyankatesh, whose joining was two days prior to today. Then we can make use of the following query statement to insert this record:

Code:

INSERT INTO 'educba_writers' ('id', 'firstName', 'rate', 'joining_date') VALUES
(2, 'Vyankatesh', 700, CURDATE()-2);

Output:

add joining date

Example #5

Let us retrieve the record and view the inserted value in the educba_writers table using the following query statement:

Code:

SELECT * FROM educba_writers;

Output:

educba_writers table

Example #6

Suppose we have to insert a writer record named Omprakash, whose joining is after four days. Then we can use the query statement as follows:

Code:

INSERT INTO 'educba_writers' ('id', 'firstName', 'rate', 'joining_date') VALUES
(3, 'Omprakash', 600, DATE_FORMAT((CURDATE() + 4), "%Y-%m-%d"));

Output:

record

Example #7

Let us retrieve the record and view the inserted value in the educba_writers table using the following query statement:

Code:

SELECT * FROM educba_writers;

Output:

view the inserted value

Alternatives of CURDATE() function

We can use the CURRENT_DATE and CURRENT_DATE() functions instead of the CURDATE() function, as both of them work in the same way and return the same value as that of the CURDATE() function. Let us confirm the working of all three functions by executing a simple query of SELECT that will retrieve the value of all three functionalities as follows:

Code:

SELECT CURDATE(), CURRENT_DATE, CURRENT_DATE();

Output:

Alternatives

Explanation: We can see that all three functions and statements retrieved the same value on 2020-05-27, which is today’s date.

Difference between NOW() function and CURDATE()

Now() function retrieves the whole timestamp that includes the current date and time, while the CURDATE() function retrieves only the current date. The date part of the NOW() function is equivalent to the CURDATE() function’s output. Let us see the result by using the following query statement:

Code:

SELECT CURDATE(), DATE(NOW()), NOW();

Output:

Difference

Explanation: We can observe that the value of the CURDATE() and DATE(NOW()) expressions is equivalent.

Conclusion

We can use MySQL’s CURDATE() function to retrieve the current date and manipulate it according to our necessity.

Recommended Articles

We hope that this EDUCBA information on “MySQL CURDATE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL Data Types
  2. SQL Date Function
  3. MySQL BIN()
  4. MySQL INSTR()
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more