Home > front end >  How to search for substring with following criterias?
How to search for substring with following criterias?

Time:09-20

**Table:**
| String        | Substring|
| --------      | ---------|
| Tattinger     | TT       |
| TT's          | TT       |
| TT Tattinger  | TT       |
| Tattinger TTa | TT       |
| TTinger tab   | TT       |

Criterias:

  1. Substring can not be in the middle of string.
  2. Substr should always be at the start of each letters
  3. If there is any non-characters('.,#) after substring, that's okay.
  4. All other cases No

Result

| String        | Substring| Result|
| --------      | ---      |-------|
| Tattinger     | TT       |No     |
| TT's tattinger| TT       |Yes    |
| TT Tattinger  | TT       |Yes    |
| Tattinger TTa | TT       |Yes    |
| TTinger tab   | TT       |Yes    |

What I've explored so far:
1. STRING LIKE '%' || SUBSTRING || '%'----(not suitable because it matches anywhere)
2. REGEXP_SUBSTR(STRING, SUBSTRING || '\\W') --(suitable partially)

CodePudding user response:

How about a simple case statement? I am using like but if case isn't important change that to ilike

case when str like 'TT%' or str like '% TT%' then 'Yes' else 'No' end as flag
  • Related