Solved

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

Posted on 2009-07-08
5
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

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 60

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 60

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Transaction Number 19 49
Need more granular date groupings 4 45
DB Shutdown Automatically 11 36
STUFF function - proper use to update column? 6 16
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

738 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