Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL compare now() (only date, not time) with a datetime field

Tags:

mysql

I have a duedate column(datetime format) that specifies the due date of a ticket, now i need to get 'Due today' tickets base on the comparison between now() with duedate. i.e 2010-04-29 02:00 vs 2010-04-29 10:00 would return true in this matter.

like image 924
user1918956 Avatar asked Jan 26 '13 04:01

user1918956


People also ask

How can I compare current date and date in MySQL database?

MySQL has the ability to compare two different dates written as a string expression. When you need to compare dates between a date column and an arbitrary date, you can use the DATE() function to extract the date part from your column and compare it with a string that represents your desired date.

Can we compare date with datetime in SQL?

The right way to compare date only values with a DateTime column is by using <= and > condition. This will ensure that you will get rows where date starts from midnight and ends before midnight e.g. dates starting with '00:00:00.000' and ends at "59:59:59.999".

How can I compare two dates in MySQL query?

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

What does NOW () return in MySQL?

The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.


2 Answers

Use DATE(NOW()) to compare dates

DATE(NOW()) will give you the date part of current date and DATE(duedate) will give you the date part of the due date. then you can easily compare the dates

So you can compare it like

DATE(NOW()) = DATE(duedate)

OR

DATE(duedate) = CURDATE() 

See here

like image 196
Bhavik Shah Avatar answered Oct 03 '22 11:10

Bhavik Shah


Compare date only instead of date + time (NOW) with:

CURDATE()
like image 21
Cae Vecchi Avatar answered Oct 03 '22 09:10

Cae Vecchi