Sam OZ
asked on
Oracle Query to get values ending in _0
I have an Oracle database with Table TB_Documents I need to get all the Docuemnts ending as _0
While I try DocNo like '%_0' , I am getting more rows than the ones I need . The _ seems to be ignored
How can I run a query to get the right result ?
While I try DocNo like '%_0' , I am getting more rows than the ones I need . The _ seems to be ignored
How can I run a query to get the right result ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks . How can make REGEXP_LIKE to see if any document ending with underscore and any alphanumeric?
For example anything ending like _1 _2 _9 ...... _a _A ............ _z _Z
For example anything ending like _1 _2 _9 ...... _a _A ............ _z _Z
For searching strings ending with an underscore and an alphanumeric, you may use
select docno
from tb_documents
where regexp_like(docno, '[_][[:alnum:]]');
you still need the $ at the end
with x as (
select 'a_1_' doc from dual union
select 'b_a' from dual union
select 'c_9' from dual union
select 'aaa' from dual)
select * from x
where regexp_like(doc, '_[[:alnum:]]$');
@Geert G - Indeed. Good spot.
>>> the regexp_like also offers a function based index search if you wrap it in a case
Yes, if you want to make a function-based index on case, then you could do so with LIKE as well.
Yes, if you want to make a function-based index on case, then you could do so with LIKE as well.
Just as a point of clarification. The reason that you get more results than you expected is because the underscore is a single character wild card to LIKE.
If someone else pointed that out, sorry, I missed it. I looked but I didn't see it.
The documentation (here), does explain it. That is the 12.2 documentation, but it has been like that as long as I have been using Oracle.
If someone else pointed that out, sorry, I missed it. I looked but I didn't see it.
The documentation (here), does explain it. That is the 12.2 documentation, but it has been like that as long as I have been using Oracle.
select * from TB_Documents
where DocNo like '%\__' escape '\';
Nore - that's two underscores, the first one is escaped meaning look for an underscore and the second one means any single character following it.
where DocNo like '%\__' escape '\';
Nore - that's two underscores, the first one is escaped meaning look for an underscore and the second one means any single character following it.
Open in new window