views:

39

answers:

2

Hello!

I am trying to modify an app for a client which has already a database of over 1000 items. The dates are stored as text in the database with the format "02/10/1984". The system allows you to add and remove fields to the catalog dynamically and it also allows the advanced search to have specific fields be allowed.

The problem is that it wasn't designed with dates in mind, so when I set a field as a date, and try to search by a range the query is trying to do a AND (cfv0.value >= 01/02/2004 AND cfv0.value <= 05/03/2008) . I can make it so the date range passed is a numeric time value. Is there a way that when sending the query, it takes the text fields (with the date) and converts it to numeric time value so at that point I am basically just comparing numbers which would work fine.

I do not have the option to change all the current date to numeric value due to the way the dynamic fields are set up.

Thanks guys!

+3  A: 

You'll want to convert both the query coming in and the date in the column, so something like this (assuming it's in month/day/year order):

AND STR_TO_DATE(cfv0.value, '%m/%d/%Y') >= STR_TO_DATE('01/02/2004', '%m/%d/%Y')
Chad Birch
a function applied to the column in a WHERE this will prevent any index usage.
KM
Yeah, it will. But there isn't really any other option that I can think of, except changing the date format to something conducive to string-comparison like YYYY-MM-DD.
Chad Birch
YYYY-MM-DD only matters for sorting. if the leading zeros are consistent, you can search just fine on the string to string. best advise would be that 1000 rows is not much, create a date column and convert the data using your STR_TO_DATE()
KM
You could also pass the str_to_date returning value to unix_timestamp to be safer with the calculations.
Jonathan Czitkovics
so this also formats the current data in the column to date format? it looks like what your doing is just converting the passed variable to a date..but not data already in the column.
Roeland
There are two calls to `STR_TO_DATE` necessary, one to operate on the column, and one on the search term.
Chad Birch
this will convert the table data to a date (for each row) and the passed in string filter value (one time) to a date, but the data is not changed in the database, it remains a string for the next slow query to slog through it.
KM
got it to work, thank you so much!
Roeland
+1  A: 

Note that in the future, if you'd like dates that are stored as a string to sort the same both alphabetically and chronologically, then store them in zero-padded, four digit year, month, day format, like this:

2004/01/20
2004/10/05
2006/12/31
Marcus Adams
or in a date column
Veger