0

I have column inside the database called proxima_cal and its varchar(5) that has a value like 11/16 that represent month/year and I need to select this table.

WHERE proxima_cal BETWEEN "11/16" AND "11/19"

as a varchar BETWEEN doesn't work so what I have to do to mysql conceder this column as date and get correct result ?

11
  • Maybe - dev.mysql.com/doc/refman/5.7/en/… ?
    – Suleiman
    Jan 28, 2016 at 15:38
  • You should fix the problem by altering the table, adding correctly defined column and then populating it. Once you migrate the data, drop the varchar column, it makes absolutely 0 sense to use it, it even spends more storage space to store 5 characters compared to a timestamp column. Every other possible "solution" will be a hack.
    – Mjh
    Jan 28, 2016 at 15:40
  • 1
    @Mjh Not much help if it's a legacy system
    – mikeyq6
    Jan 28, 2016 at 15:41
  • 1
    @mikeyq6 - of course it is much help. Every other solution isn't a solution, it's a hack and will work limited amount of life of the application. Problems are either solved or not, sticking them under carpet doesn't make them solved. The actual solution is to change the data type and migrate current data. I know people work with legacy systems, but you could use that argument for every single thing out there and we'd never have anything working.
    – Mjh
    Jan 28, 2016 at 15:55
  • @Mjh I don't think telling people what they 'should' do is solving their problem. What if they aren't able to change the structure of the database?
    – mikeyq6
    Jan 28, 2016 at 15:59

1 Answer 1

1

You can use the STR_TO_DATE method, something like:

WHERE STR_TO_DATE(CONCAT('01/', proxima_cal), '%d/%m/%y') BETWEEN '2016/11/01' AND '2019/11/01'
1
  • i changed the code because its 11-16 not 11/16 so i made like that WHERE STR_TO_DATE(CONCAT('01-', proxima_cal), '%d-%m-%y') BETWEEN '2016-11-01' AND '2019-11-01'
    – Mina Magdy
    Jan 28, 2016 at 16:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.