[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PL/SQL Search for multiple strings

Posted on 2016-11-22
5
Medium Priority
?
173 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:Allen Pitts
[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 2000 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:Allen Pitts
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

650 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