开发者

SQL and regular expression to check if string is a substring of larger string?

I have a database filled with some codes like

EE789323
     990
   78000

These numbers are ALWAYS endings of a larger code. Now I have a function that needs to check if the larger code contains the subcode.

So if I have codes 90 and 990 and my full code is EX888990, it should match both of them.

However I need to do it in t开发者_JAVA百科he following way:

SELECT * FROM tableWithRecordsWithSubcode
 WHERE subcode MATCHES [reg exp with full code];

Is a regular expression like this this even possible?

EDIT:

To clarify the issue I'm having, I'm not using SQL here. I just used that to give an example of the type of query I'm using. In fact I'm using iOS with CoreData, and I need a predicate to fetch me only the records that match. In the way that is mentioned below.


Given the observations from a comment:

Do you have two tables, one called tableWithRecordsWithSubcode and another that might be tableWithFullCodeColumn? So the matching condition is in part a join - you need to know which subcodes match any of the full codes in the second table? But you're only interested in the information in the tableWithRecordsWithSubcode table, not in which rows it matches in the other table?

and the laconic "you're correct" response, then we have to rewrite the query somewhat.

SELECT DISTINCT S.*
  FROM tableWithRecordsWithSubcode AS S
  JOIN tableWithFullCodeColumn     AS F
    ON F.Fullcode ...ends-with... S.Subcode

or maybe using an EXISTS sub-query:

SELECT S.*
  FROM tableWithRecordsWithSubcode AS S
 WHERE EXISTS(SELECT * FROM tableWithFullCodeColumn AS F
               WHERE F.Fullcode ...ends-with... S.Subcode)

This uses a correlated sub-query but avoids the DISTINCT operation; it may mean the optimizer can work more efficiently.

That just leaves the magical 'X ...ends-with... T' operator to be defined. One possible way to do that is with LENGTH and SUBSTR. However, SUBSTR does not behave the same way in all DBMS, so you may have to tinker with this (possibly adding a third argument, LENGTH(s.subcode)):

LENGTH(f.fullcode) >= LENGTH(s.subcode) AND
SUBSTR(f.fullcode, LENGTH(f.fullcode) - LENGTH(s.subcode)) = s.subcode

This leads to two possible formulations:

SELECT DISTINCT S.*
  FROM tableWithRecordsWithSubcode AS S
  JOIN tableWithFullCodeColumn     AS F
    ON LENGTH(F.Fullcode) >= LENGTH(S.Subcode)
   AND SUBSTR(F.Fullcode, LENGTH(F.Fullcode) - LENGTH(S.Subcode)) = S.Subcode;

and

SELECT S.*
  FROM tableWithRecordsWithSubcode AS S
 WHERE EXISTS(
       SELECT * FROM tableWithFullCodeColumn AS F
        WHERE LENGTH(F.Fullcode) >= LENGTH(S.Subcode)
          AND SUBSTR(F.Fullcode, LENGTH(F.Fullcode) - LENGTH(S.Subcode)) = S.Subcode);

This is not going to be a fast operation; joins on computed results such as required by this query seldom are.


I'm not sure why you think that you need a regular expression... Just use the charindex function:

select something
from table
where charindex(code, subcode) <> 0

Edit:

To find strings at the end, you can create a pattern with the % wildcard from the subcode:

select something
from table
where '%' + subcode like code
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜