Solved

How to get this query ?

Posted on 2014-09-04
8
259 Views
Last Modified: 2014-09-05
Hi  Expert,
I am using oracle 11.1.2.0

below is my table

create table sample_test(e_id number(10), e_name varchar2(4000));

insert into SAMPLE_TEST (E_ID,E_NAME) values (103664,'XYZ., Ltd.');
insert into sample_test (e_id,e_NAME) values (103665,'XYZ., Ltd.!@#$%^&*()_+=-{}][|\'';":/?.>,<');

Open in new window

Query:
select * from  SAMPLE_TEST
where id in (103664,
 103665)
and regexp_instr(E_NAME
                    ,nvl(:Name
                        ,E_NAME)
                    ,1
                    ,1
                    ,1
                    ,'im') > 0; 

Open in new window



when i am running  above query(i am passing null value in parameter) , i am getting below error

ORA-12726: unmatched bracket in regular expression
12726. 00000 -  "unmatched bracket in regular expression"
*CAUSE:    the Regular Expression Did Not Have Balanced Brackets.
*Action:   Ensure the brackets are correctly balanced.

Regards
Thomos
0
Comment
Question by:deve_thomos
  • 6
8 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40303768
Using E_NAME as the pattern to regexp_instr it has to be a valid pattern.

This row has brackets in the pattern:
insert into sample_test (e_id,e_NAME) values (103665,'XYZ., Ltd.!@#$%^&*()_+=-{}][|\'';":/?.>,<');

You need to escape them:
insert into sample_test (e_id,e_NAME) values (103665,'XYZ., Ltd.!@#$%^&*()_+=-{}\]\[|\'';":/?.>,<');

that said:
What you posted really doesn't make sense.

What are you trying to do?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40303773
change this

'XYZ., Ltd.!@#$%^&*()_+=-{}][|\'';":/?.>,<'

to this

'XYZ., Ltd.!@#$%^&*()_+=-{}]\[|\'';":/?.>,<'


the problem is the [ which starts a list expression, if you don't intend to create one then you'll need to escape it with the slash


or,  more likely, since your NVL looks like you are trying to do a wild card self-match then try this instead and then you won't have to modify your data


SELECT *
  FROM sample_test
 WHERE e_id IN (103664, 103665)
   AND REGEXP_INSTR(
           e_name,
           NVL( :name, '.'),
           1,
           1,
           1,
           'im'
       ) > 0;
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40303792
another option, check for null explicitly outside of the regexp function


SELECT *
  FROM sample_test
 WHERE e_id IN (103664, 103665)
   AND ( :name IS NULL
     OR REGEXP_INSTR(
            e_name,
            :name,
            1,
            1,
            1,
            'im'
        ) > 0)
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40303799
And one more suggestion,  rather  than using regexp_instr, you might want to try regexp_like for a little simpler syntax


SELECT *
  FROM sample_test
 WHERE e_id IN (103664, 103665) AND ( :name IS NULL OR REGEXP_LIKE(e_name, :name, 'im'))


Note - both this suggestion and the previous one with the external NULL check should be more efficient but have the side effect that they will match NULL e_names as well which the "." version I suggested first will not.

Assuming your e_names are not null, then they are all functionally equivalent
0
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.

 
LVL 73

Expert Comment

by:sdstuber
ID: 40303839
one more thing that occured to me

searching mid-string is not an efficient operation because it's not something you can build a normal index for.

If your data is big, you might want to look into using a text index and changing the query to use CONTAINS.

CONTAINS isn't the same thing as a regular expression parser but it could give better performance for basic substring searches
0
 

Author Closing Comment

by:deve_thomos
ID: 40304060
Thanks lot....
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40304071
are you sure you want to try to modify your data?

adding the slash in this one example fixes that one row.

if you have other characters, for example the "." following "Ltd."   that period is not actually a period for the purposes of your matching.  It's really a wild card

 Is that really what you intended?  If not, then you probably don't want to go with a row-at-a-time data fix strategy.  Instead, try fixing the query with one of the options suggested.

or, maybe I'm misunderstanding what you were really looking for because the accepted answer does not seem correct for a real-world solution.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40306520
Based on your coworker's question here :

http://www.experts-exchange.com/Q_28511647.html

It looks like you two are pursuing CONTAINS rather than corrupting your data as a solution.

 That's probably a good idea but it would indicate this question was closed improperly
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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

743 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

12 Experts available now in Live!

Get 1:1 Help Now