EDUCBA

EDUCBA

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

MySQL today()

Aanchal Sharma
Article byAanchal Sharma
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated May 29, 2023

MySQL-today()

Introduction to MySQL today()

In MySQL, the Today() function is a built-in MySQL Date function that provides today’s date value, where we will be using MySQL stored procedures and functions. With this MySQL Today() function, we will learn to create a special type of MySQL stored program by implementing the CREATE FUNCTION query statement.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

In MySQL Today() function, we will build a Date function using some of the MySQL Date functions to access the single Date value, thus called a built-in date utility code. In general, developers use stored functions in MySQL to summarize mutual formulas or implement specific rules that can be reused across multiple SQL statements or stored programs.

Syntax

To get MySQL Today() function to result in today’s date, we will use the built-in functions of the date data type. We must create your own MySQL Today() function as a stored program. As we know CURDATE() function or CURRENT_DATE() function in MySQL provides the current date, and we also use it most of the time to retrieve the current date or time in MySQL queries.

Now, we can replace this MySQL CURDATE() function or CURRENT_DATE() function with MySQL Today() function to make the query statements more comprehensible. This is possible by the succeeding SQL command code structure firstly:

DELIMITER $$     // creating stored function program
CREATE FUNCTION FunctionName(
Parameter1,
Parameter2,….
)
RETURNS Data_Type   // Declare the datatype
[NOT] DETERMINISTIC
BEGIN
RETURN  Replaceable_Stored_FunctionName;   // statements
END$$
DELIMITER ;    // end of the procedure

Explaining the terms of the above syntax below:

  • FunctionName specifies the name we give to the stored function when using the CREATE FUNCTION keyword to name and create it.
  • The stored function name followed us; we need to list all the parameters we wanted to mention within the parentheses. All these parameters are the MySQL IN parameters by default, so we cannot state OUT, INOUT, or IN modifiers to those parameters.
  • With the RETURNS statement, we will specify the valid MySQL Data type for which we are creating the stored program function.
  • Then, we must state the DETERMINISTIC keyword to make the function either deterministic or not. In a deterministic function, it also returns the identical result for similar parameters as inputs, whereas, in a non-deterministic one, it outputs different values for similar parameters as inputs. But MySQL uses the NOT DETERMINISTICkeyword option by default if we do not set anyone.
  • Between the BEGIN and END keyword blocks, we can write the code in this body for the stored function. In the body block section, at least a single RETURN query statement should be mentioned, which returns the result value to the calling function. On execution, the stored function terminates instantly if the RETURN query is visited.

After executing the properly stored program syntax above, using valid names for functions, parameters, and data types, the stored function will be created in the MySQL database. We can call the newly built stored program with the following MySQL query and execute the output as desired:

SELECT FunctionName();

The term “FunctionName” is mentioned in the above syntax of the stored program function.

How does MySQL Today() Function work?

Let us consider accessing today’s date by implementing the built-in date functions to provide the current date in the MySQL database.

Sometimes, from the database table rows, we want to fetch data having a date column with the Date data type as today, and then the query will be written as:

SELECT ColumnName FROM TableName WHERE ExpiredDate = Today;

To acquire today’s date, we can use either the CURRENT_DATE() or CURDATE() function with the query as follows:

SELECT CURRENT_DATE() Today;

Output:

MySQL today() - 1

Also, you can use the NOW() function or SYSDATE() function to select the date part from the present date-time resulting by the function as shown follows:

SELECT DATE(SYSDATE()) Today;

Output:

MySQL today() - 2

Now, we can alter the query with the following one:

SELECT ColumnName FROM TableName WHERE ExpiredDate = CURRENT_DATE();

But suppose the column ExpiredDate includes both date and time types; then you can apply DATE() in MySQL to get them just the date value and equate it with the current value of date:

SELECT ColumnName FROM TableName WHERE DATE(ExpiredDate) = CURRENT_DATE();

Examples to Implement Today() function in MySQL

To understand MySQL Today(), we can replace the CURRENT_DATE() function when we use this function many times in the queries.

For this, let us create a stored function named Today() in MySQL with the syntax stated below:

DELIMITER $$
CREATE FUNCTION Today()
RETURNS DATE
BEGIN
RETURN  CURRENT_DATE();
END$$
DELIMITER ;

Execute the above command in the MySQL database, and you will see that the Today() function is created as a stored program. Now, you can call and utilize it in your query statements.

To implement the created program, we will run the following query with the Today() function along with the MySQL SELECT keyword to fetch the current value of the date:

SELECT Today()Today_Date;

Output:

Using Select query - 3

Now, with this MySQL Today() function, a built-in date function with the stored program to output the current date, we can use it to fetch the date of the next day, i.e., tomorrow or more upcoming days. The query can be written as simply as the code below to get tomorrow’s date or the day after tomorrow’s date:

SELECT Today() + interval 2 day Day_after_tomorrow_date;

Output:

MySQL today() - 4

Here, the interval keyword determines the interval value and unit.

Again, let us consider fetching the data which is of either the previous day or, say, yesterday, using the MySQL Today() function as follows:

SELECT Today() -interval 2 day Day_before_yesterday_date;

Output:

Day before yesterday - 5

Conclusion

Likewise, we can also learn and develop the desired function of date or other valid data types in MySQL by applying the stored program and execute it and use it in our MySQL queries. Thus, The MySQL Today() function is a unique stored program we create and query to obtain the current date values. We can use it in MySQL statements to provide the desired output.

Recommended Articles

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

  1. MySQL Timestamp
  2. Working of MySQL Avg()
  3. MySQL MAX() Function
  4. NOT In MySQL
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MYSQL Course Bundle - 18 Courses in 1 | 3 Mock Tests
 93+ Hour of HD Videos
18 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
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