?
Solved

How to get this query ?

Posted on 2014-09-04
8
Medium Priority
?
300 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
[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
  • 6
8 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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 74

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 74

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 74

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
 
LVL 74

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 74

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 74

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

777 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