Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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

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
MeridianManagement
Asked:
MeridianManagement
  • 3
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
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
 
MeridianManagementAuthor Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
MeridianManagementAuthor Commented:
beautiful, thank you, worked like a charm!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now