tags:

views:

74

answers:

3

I have a mysql table :

dateStarted     varchar(45)
dateEnded       varchar(45)

Example:

alt text

There are varchar (I dont know why Previous programmer used this).

Can I search between two dates in mysql with varchar type?

Thanks in advance

+8  A: 

Nope. You have to change format to a proper one.

Previous programmer 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 big part of every programmer's worktime.

Col. Shrapnel
Yup, take some pain now and convert it. You'll have some horrible performance issues hacking between strings and dates.
nonnb
You are right, But could DD-MM-YYYY be a permitted date format?
phpExe
@phpExe not in mysql. mysql date format is `YYYY-MM-DD`
Col. Shrapnel
@Col. Shrapnel , I have changed the database, Thanks!
phpExe
+2  A: 
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.

kander
@kander, If I change the type o column, DD-MM-YYYY formate is not suppurted by mysql. Because the date format maybe in DD-MM-YYYY.
phpExe
A: 

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.

Tim McNamara
Agree with @Col. Shrapnel though, you should take the time to store the dates natively.
Tim McNamara