Solved

PL/SQL Search for multiple strings

Posted on 2016-11-22
5
114 Views
Last Modified: 2016-11-22
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
Comment
Question by:9apit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 41897825
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
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 41897837
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
 
LVL 74

Expert Comment

by:sdstuber
ID: 41897846
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
 

Author Closing Comment

by:9apit
ID: 41897853
The regular expression seems like a good solution. Thanks.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41897886
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question