0

I had the question for the mysql date between select. In the mysql table, the field is varchar. The date range is '21-01-2013' and '31-01-2013', it can show the records, but the date range is '21-01-2013' and '20-02-2013', it cannot show the records.

"SELECT * from away_from_office where (awaydatefrom between '21-01-2013' and '31-01-2013') ";
2
  • the between operation, cant work on strings, as you mentioned; you need to convert the string to integer value, as shown by the JW user Commented Jan 3, 2013 at 10:07
  • 2
    The solution is to switch to a DATE field not VARCHAR. As answers show you can convert on the fly but that's more of a quickfix/patch than a proper solution. Commented Jan 3, 2013 at 10:07

2 Answers 2

2

you should convert it to date first using STR_TO_DATE, eg.

SELECT * 
from away_from_office 
where awaydatefrom between STR_TO_DATE('21-01-2013', '%d-%m-%Y') and  
                           STR_TO_DATE('31-01-2013', '%d-%m-%Y') 

if the column has the same format with the one you've shown, convert it also

WHERE STR_TO_DATE(awaydatefrom, '%d-%m-%Y') BETWEEN

if you have a chance to alter the table, or you're working with sample records, alter your table by changing the data type of the column to DATE.

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

1 Comment

If you feel an answer solved the problem, please mark it as 'accepted' by clicking the white check mark (around 15mins to an hour later). This helps keep the focus on older SO which still don't have answers.
1

Change column datatype to DATE

http://dev.mysql.com/doc/refman/5.1/en/datetime.html

Comments

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.