mysql: searching BETWEEN dates stored as varchar

后端 未结 6 1276
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 14:20

i would like to select * from table where dates between (some_date and another_date)

the problem is that the dates are stored as varchar!

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 14:57

    IF you are using SQl use this query

    Data

    DECLARE @Dates TABLE (StartDate varchar(100));
    
    INSERT INTO @Dates VALUES ('7/1/2010 9:10 AM');
    INSERT INTO @Dates VALUES ('7/5/2010 10:33 AM');
    INSERT INTO @Dates VALUES ('7/13/2010 04:53 AM');
    INSERT INTO @Dates VALUES ('7/22/2010 8:45 AM');
    INSERT INTO @Dates VALUES ('7/10/2010 11:20 AM');
    INSERT INTO @Dates VALUES ('7/11/2010 12:40 AM');
    

    Query:

    SELECT * FROM @Dates
    WHERE (CONVERT(datetime,StartDate,101) >= CONVERT(datetime,'7/1/2010 9:10 AM',101))
                                             AND (CONVERT(datetime,StartDate,101) <= CONVERT(datetime,'7/15/2010 9:10 AM',101))
    ORDER BY CONVERT(datetime,StartDate,101)
    

提交回复
热议问题