Solved

PL/SQL Search for multiple strings

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
help on oracle query 5 53
Use of Exception to end a Loop 3 45
Oracle Insert not working 10 32
Database Design Dilemma 6 58
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

827 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