Solved

PL/SQL Search for multiple strings

Posted on 2016-11-22
5
20 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
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 73

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 73

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 31

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to recover a database from a user managed backup

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now