Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql date comparison with date_format

Tags:

I googled and tried several ways to compare date but unfortunately didn't get the result as expected. I have current state of records like following:

        mysql> select date_format(date(starttime),'%d-%m-%Y') from data;                +-----------------------------------------+               | date_format(date(starttime),'%d-%m-%Y') |               +-----------------------------------------+               | 28-10-2012                              |               | 02-11-2012                              |               | 02-11-2012                              |               | 02-11-2012                              |               | 03-11-2012                              |               | 03-11-2012                              |               | 07-11-2012                              |               | 07-11-2012                              | 

I would like to compare date and therefore do like this:

        mysql> select date_format(date(starttime),'%d-%m-%Y') from data where date_format(date(starttime),'%d-%m-%y') >= '02-11-2012';                +-----------------------------------------+                | date_format(date(starttime),'%d-%m-%Y') |                +-----------------------------------------+                | 28-10-2012                              |                | 02-11-2012                              |                | 02-11-2012                              |                | 02-11-2012                              |                | 03-11-2012                              |                | 03-11-2012                              |                | 07-11-2012                              |                | 07-11-2012                              | 

I believe that the result should not include '28-10-2012'. Any suggestion? Thanks in advance.

like image 817
Doni Andri Cahyono Avatar asked Nov 22 '12 07:11

Doni Andri Cahyono


People also ask

How can I compare two date fields 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.

Can you compare dates in MySQL?

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 you use comparison operators on dates in SQL?

We can compare dates using Comparison Operators in SQL like, = (Equals), < (Less than), > (Greater than), <= (Less than Equal), >= (Greater than Equal), <> (Not Equal), etc.

How can I calculate days difference between two dates in MySQL?

The DATEDIFF() function returns the number of days between two date values.


1 Answers

Your format is fundamentally not a sortable one to start with - you're comparing strings, and the string "28-10-2012" is greater than "02-11-2012".

Instead, you should be comparing dates as dates, and then only converting them into your target format for output.

Try this:

select date_format(date(starttime),'%d-%m-%Y') from data where date(starttime) >= date '2012-11-02'; 

(The input must always be in year-month-value form, as per the documentation.)

Note that if starttime is a DATETIME field, you might want to consider changing the query to avoid repeated conversion. (The optimizer may well be smart enough to avoid it, but it's worth checking.)

select date_format(date(starttime),'%d-%m-%Y') from data where starttime >= '2012-11-02 00:00:00'; 

(Note that it's unusual to format a date as d-m-Y to start with - it would be better to use y-M-d in general, being the ISO-8601 standard etc. However, the above code does what you asked for in the question.)

like image 122
Jon Skeet Avatar answered Oct 01 '22 23:10

Jon Skeet