• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

PL/SQL Search for multiple strings

Hello Expert,

It has been discovered that special characters (! @ # $ % ^ & *) in the field Protocol_Name
breaks some of our code.
So effort is underway to find special characters in Protocol_Name s and work w the business to correct them.

When looking for a number in a field like PROTOCOL_ID a WHERE  a statement can be employed like:
WHERE PROTOCOL_ID in (704759, 1687076, 1732188, 618246, 733428)

In looking for string is there a way of looking for multiple strings?

This works but seems awkward.

WHERE POTOCOL_NAME like ('%' || '!' || '%')
OR PROTOCOL_NAME like ('%' || '@' || '%')
OR PROTOCOL_NAME like ('%' || '#  || '%')
OR PROTOCOL_NAME like ('%' || '$ || '%')
OR PROTOCOL_NAME like ('%' || '% || '%')
.....

Is there a way of drafting a single clause, like the IN operator
for multiple numbers, that works for multiple strings?

Thanks.

Allen in Dallas
0
Allen Pitts
Asked:
Allen Pitts
1 Solution
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
try this:

select protocol_name
from protocol_table
where length(protocol_name)!=length(translate(protocol_name,'1234567890','1234567890'));

this query will get you all records with protocol names which have anything other than digits. It could be a character or a symbol like !, @, $ etc

Thanks,
0
 
sdstuberCommented:
A middle-of-string search is always going to be inefficient.
your OR conditions are probably the best option  (except for this :  PROTOCOL_NAME like ('%' || '%' || '%'), because % is the wild card so it will match everything)

but if you're looking for something compact you can try these:

SELECT *
  FROM your_table
 WHERE REGEXP_LIKE(protocol_name, '[!@#$%]');

SELECT *
  FROM your_table
 WHERE TRANSLATE('x' || protocol_name, 'x!@#$%', 'x') != 'x' || protocol_name



I append the 'x' in the second example, because a string with nothing but the search characters would get translated to NULL which would then fail the search.  Adding the dummy character ensures every string will be searched correctly.

The LENGTH() check in the first post would also fail with NULL reduction because length('') is not 0, it's null
0
 
sdstuberCommented:
also note, the usage of TRANSLATE in the first post doesn't work because it doesn't actually alter anything to produce a functional comparison

For example:

SELECT TRANSLATE('1234', '1234567890', '1234567890') FROM DUAL
UNION ALL
SELECT TRANSLATE('abcd', '1234567890', '1234567890') FROM DUAL
UNION ALL
SELECT TRANSLATE('!@#$%', '1234567890', '1234567890') FROM DUAL
0
 
Allen PittsBusiness analystAuthor Commented:
The regular expression seems like a good solution. Thanks.
0
 
awking00Commented:
As a corollary to sdstuber's solution, you could also search where the protocol_name is not all numeric -
where regexp_like(protocol_name,'[^[:digit:]]')
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now