Chinnappa
Chinnappa

Reputation: 37

MYSQL DATE range Query with date format

I have a MYSQL table with field name "date" in the formate "dd/mm";

I need to query results from 20/06 to 22/06 (3 days data), this is how I am trying to do it.

mysql> SELECT * FROM `wtt` WHERE date BETWEEN '20/06' AND '22/06';

But MYSQL is also selecting the dates 22/04, 22/05 and other entries.

Is there MYSQL query which will fetch me the right answer without the need to change the format in which I have already stored the information in the MYSQL table?

Thank you all in advance!

Upvotes: 1

Views: 541

Answers (1)

Jan
Jan

Reputation: 2293

You should convert the dates to MySQL date types and then compare them.

You can convert the dates like this:

SELECT STR_TO_DATE(CONCAT('20/06', '/', EXTRACT(YEAR FROM CURDATE())),'%d/%m/%Y');

or simply

SELECT STR_TO_DATE('20/06','%d/%m');

for year 0000 if it doesn't matter.

Storing dates as strings has brought this problem on you. You should always use DATE type for storing dates.

Upvotes: 3

Related Questions