Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if datetime equal tomorrow in MySQL

I want to retrieve mysql table data if the data created_at Datetime column equal to tomorrow date, for example:

SELECT * FROM sales_order where created_at = tomorrow_date; 
like image 982
mileven Avatar asked Apr 17 '18 07:04

mileven


People also ask

How can I get tomorrow date in MySQL?

To get the yesterday and tomorrow of the current date we can use the CURRDATE() function in MySQL and subtract 1 from it to get yesterday and add 1 to it to get tomorrow.

What is MySQL Curdate?

MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.

How can I get date between two dates in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.

How do I check if a date is greater than today in SQL?

GETDATE() function: This function is used to return the present date and time of the database system. After comparison column contains the following string: Lesser than- If the date is less than today's date. Greater- If the date is greater than today's date.


2 Answers

You can use the following solution, using DATEDIFF and DATE_ADD:

SELECT * 
FROM sales_order 
WHERE DATEDIFF(created_at, DATE_ADD(CURDATE(), INTERVAL 1 DAY)) = 0;

or a simpler solution only using DATEDIFF:

SELECT * 
FROM sales_order 
WHERE DATEDIFF(created_at, CURDATE()) = 1

DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation. - from MySQL docs.

like image 172
Sebastian Brosch Avatar answered Sep 21 '22 11:09

Sebastian Brosch


SELECT * FROM sales_order where created_at = CURDATE() + 1;
like image 25
Shaam Avatar answered Sep 19 '22 11:09

Shaam