Solved

changing date format to compare dates on the fly in mysql query

Posted on 2009-07-08
5
298 Views
Last Modified: 2012-05-07
I have a database where the dates are in mm.dd.yy format. I need to compare dates to other dates that are in the same format. All I need to know is if one date comes before the other. How can I compare them using a query when they are in this format?
I want to do
 

select * from table where date > date2

Open in new window

0
Comment
Question by:MeridianManagement
  • 3
  • 2
5 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24804912
Are the fields actually stored as date data type:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

Or is this a character field that stores a date?
0
 
LVL 2

Author Comment

by:MeridianManagement
ID: 24804937
no, they're stored as varchar in "mm.dd.yy" format. So, "07.01.09" is an example of July 1st 2009. Otherwise I wouldn't even need to ask this question to compare dates as it would be very simple.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24804942
If it is string, you can try using STR_TO_DATE function on both dates and then the comparison as you have codes will work just fine.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24804974
Example usage:
SELECT * 

FROM table 

WHERE STR_TO_DATE(date, "%m.%d.%y") > STR_TO_DATE(date2, "%m.%d.%y");

Open in new window

0
 
LVL 2

Author Closing Comment

by:MeridianManagement
ID: 31601173
beautiful, thank you, worked like a charm!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now