Search code examples
mysqlselectbetweensql-like

Is it possible to use the LIKE statement in inside BETWEEN statement?


Here's the thing, im trying to list a bunch of reports in a specific range, the tricky part is that we have 12 different kind of reports, so i need to be able to list EVERY report of that range.

Let me explain it with an example:

type 1 =RR10000
type 2 =TT10000
type 3 =HH10000

if i list 10000 to 10005:

RR10001
TT10001
HH10001
RR10002
TT10002
HH10002
RR10003
TT10003
HH10003
RR10004
TT10004
HH10004
RR10005
TT10005
HH10005

Is there any way to do this? any hint would be appreciated.


Solution

  • Assuming your types are always 2 characters followed by digits, then you could just extract the digits and do a between on those:

    ... WHERE SUBSTR(yourfield, 2) BETWEEN 10001 AND 10005
          AND SUSBSTR(yourfield, 0, 2) IN ('RR', 'HH', 'TT')
    

    won't be particulary efficient, since you can't use indexes for this kind of thing. If you need to do this on a "large" number of records, consider splitting this type field into a composite field (chars,digits), then you can trivially do a between on the digits field.