1

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I understand now, the above query results "0000-06-20". How would the query be to select the results to for a range of dates? I tried many combination none of them worked :( I'm new to this, thank you for your patience.
I got it, SELECT * FROM wtt WHERE STR_TO_DATE(date,'%d/%m') BETWEEN '0000-06-20' AND '0000-06-22'; Thank you a ton!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.