searching data between dates stored in varchar in mysql

微笑、不失礼 提交于 2019-11-29 10:59:29

Try with this. You can input date in dd/mm/yyyy format as in your question...

SELECT * FROM activity_emp
WHERE STR_TO_DATE(server_date_time, '%d/%m/%Y')
  BETWEEN STR_TO_DATE('29/08/2012', '%d/%m/%Y')
    AND STR_TO_DATE('07/10/2012', '%d/%m/%Y')

Update: I strongly recommend you to change datatype from VARCHAR to DATETIME

Cheers!!!

Try this one -

SELECT * FROM activity_emp
WHERE STR_TO_DATE(server_date_time, '%d/%m/%Y')
  BETWEEN '2012-09-29' AND '2012-09-30'

But it is better to store server_date_time in DATETIME data type so that MySQL can use index.

STR_TO_DATE is enough. DATE_FORMAT changes it back to VARCHAR

SELECT...
FROM...
WHERE str_to_date(substr(server_date_time,1,10),'%d/%m/%Y') 
         BETWEEN '29/09/2012' AND '07/10/2012'

when dealing date please use DATE or DATETIME data type. This will avoid you from doing casting which affects the performance of the query.

If you are storing the dates always in dd/mm/yyyy in the column then this is an easy task:

SELECT * from activity_emp 
where server_date_time BETWEEN '29/09/2012' AND '07/10/2012'

Isn't this enough? Hope it helps you

SELECT * 
FROM  `activity_emp` 
WHERE STR_TO_DATE(  `column_name` ,  '%d/%m/%Y' ) 
BETWEEN  '2010-10-10'
AND  '2010-11-10'

This is Simple and Easy to understand Date Query to search between Two Dates

Date that stored in datebase in like 01/01/2016 and datatype is varchar
SELECT * FROM `test` WHERE STR_TO_DATE(`test`.`date`, '%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-01-31' ORDER BY `test`.`date` ASC
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!