Solved

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

Posted on 2009-07-08
5
297 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now