I have a MySQL table:
dateStarted varchar(45)
dateEnded varchar(45)
Example:
alt text http://img8.imageshack.us/img8/765/dated.jpg
There is varchar开发者_StackOverflow (I don't know why the previous software engineer used this).
Can I search between two dates in MySQL with the varchar type?
Nope. You have to change the format to a proper one.
The previous software engineer used this because of ignorance. You have to correct his mistake.
Don't be afraid of some extra work. Actually it's part of every programmer's job. There is no code in the whole world, which is perfect forever. Constant code improving is called refactoring
and take a big part of every software engineer's worktime.
SELECT
*
FROM test1
WHERE STR_TO_DATE(:date, '%d-%m-%Y') BETWEEN
STR_TO_DATE(dateStart,'%d-%m-%Y') AND
STR_TO_DATE(dateEnd,'%d-%m-%Y')
Just tried this on your dataset, and it works AFAICT.
But you should of course heed the advice given by the others here, and try to fix your predecessor's mistakes, if at all possible within time-constraints. This will become an issue at some point, due to the amount of casting going on.
You will need to determine the format that the two fields are in. Are they seconds from UNIX epoch? Are they YYYYMMDD format? Once you've done that, it would be fairly easy to convert it to a date and compare them.
精彩评论