searching data between dates stored in varchar in mysql

后端 未结 6 1136
我寻月下人不归
我寻月下人不归 2020-12-18 05:45

I am storing my dates in column server_date_time in varchar in dd/mm/yyyy format and i want to fetch the records lying between some dates so i have

相关标签:
6条回答
  • 2020-12-18 06:15

    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.

    0 讨论(0)
  • 2020-12-18 06:18
    SELECT * 
    FROM  `activity_emp` 
    WHERE STR_TO_DATE(  `column_name` ,  '%d/%m/%Y' ) 
    BETWEEN  '2010-10-10'
    AND  '2010-11-10'
    
    0 讨论(0)
  • 2020-12-18 06:26

    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
    
    0 讨论(0)
  • 2020-12-18 06: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!!!

    0 讨论(0)
  • 2020-12-18 06:30

    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.

    0 讨论(0)
  • 2020-12-18 06:31

    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

    0 讨论(0)
提交回复
热议问题