1

I need to select items in a table when they match the following criteria:

1/2 TON TRCK W/TOOLS (A) or 1/2 TON TRCK W/TOOLS (B)

But DOES NOT MATCH 3/4-1 TON TRUCK W/TOOLS or any other combination.

I've tried using wildcards.

Select *
FROM Table
WHERE UNIT_ID LIKE '1/2 TON TRCK W/TOOLS (A)'

This does not work. The only thing that seems to work is using '%Tools'. Unfortunately this pulls in other records that I do not want.

I've also tried using REGEX

WHERE regexp_like (unit_id, ' ^ (1/2 TON TRCK W/TOOLS) ')
1
  • May be you misspelled TRCK? Try using TRUCK.. Commented Aug 28, 2018 at 14:37

4 Answers 4

2

At the risk of beating a dead horse, you can use regexp_like, just escape the parenthesis as they have special meaning in the regex 'language' and use the pipe symbol for an OR. This regex specifically looks for an 'A' or a 'B' character (denoted by the square brackets) inside of literal parenthesis.

SQL> with tbl(str) as (
     select '1/2 TON TRCK W/TOOLS (A)' from dual union all
     select '1/2 TON TRCK W/TOOLS (B)' from dual union all
     select '3/4-1 TON TRUCK W/TOOLS'  from dual
   )
   select str
   from tbl
   where regexp_like(str, '1/2 TON TRCK W/TOOLS \([A|B]\)');

STR
------------------------
1/2 TON TRCK W/TOOLS (A)
1/2 TON TRCK W/TOOLS (B)

SQL>
Sign up to request clarification or add additional context in comments.

Comments

0

You don't have a wildcard in your example though, it should be

Select *
FROM Table
WHERE UNIT_ID LIKE '1/2 TON TRCK W/TOOLS (%)'

Like won't work without a % or _

5 Comments

that would not work if a string is e.g. like '1/2 TON TRCK W/TOOLS x' it should only be matched with (A) and (B)
only (A) and (B) or some other things you haven't mentioned yet? If the former then the IN clause above will work fine
This worked. I assumed it was choking on the "/". As HotFix mentioned it does not capture the following characters but in my case this is acceptable.
I would prefer to use the IN command or REGEX but they do not seem to accept wildcards
But I thought you also needed it to match '3/4-1 TON TRUCK W/TOOLS'? By the way, only like accepts wildcards % and _. The regexp_ functions use regex syntax.
0
Select *
FROM Table
WHERE UNIT_ID in('1/2 TON TRCK W/TOOLS (A)', '1/2 TON TRCK W/TOOLS (B)')

1 Comment

pls add some explanation to the query to have better understanding : Review
0

REGEX does work if I include a wildcard for the parenthesis around the A

WHERE regexp_like (unit_id, '(1/2 TON TRCK W/TOOLS.A.)')

or

WHERE UNIT_ID LIKE '1/2 TON TRCK W/TOOLS (%)'

as kayakpim pointed out

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.