Home » SQL & PL/SQL » SQL & PL/SQL » instr vs DBMS_LOB.instr (11g, Linux)
- icon3.gif  instr vs DBMS_LOB.instr [message #546930] Sat, 10 March 2012 05:49 Go to next message
bigdataboy
Messages: 3
Registered: March 2012
Junior Member
Hi everyone.
Straight to the point: I need to search a pattern backwards in a CLOB field.
Function DBMS_LOB.instr does not work with '-1' offset (where to start the search) as instr does.

Parameters: instr(text_to_be_searched, pattern, offset, nth)

Example: I want to search 'Hello world' for the first instance of the letter 'o' starting from the end, backwards.
As you can see, result for DBMS_LOB.instr is null when entered -1 for offset.

select
 DBMS_LOB.instr('Hello world','o',-1,1) lob_i,
 instr('Hello world','o',-1,1) std_i
from dual;


How can I work it out?
Thanks a lot.
- Re: instr vs DBMS_LOB.instr [message #546934 is a reply to message #546930] Sat, 10 March 2012 09:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Use INSTR.

Regards
Michel
- Re: instr vs DBMS_LOB.instr [message #546967 is a reply to message #546934] Sat, 10 March 2012 15:49 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9105
Registered: November 2002
Location: California, USA
Senior Member
Although in current versions you can use INSTR on LOBs, you usually get better performance from DBMS_LOB.INSTR on LOBs. You can use REVERSE and DBMS_LOB.GETLENGTH to enable you to use DBMS_LOB.INSTR as shown below, but I suspect that may reduce performance enough that you are better off with just INSTR.

SCOTT@orcl_11gR2> select
  2    DBMS_LOB.instr ('Hello world', 'o', -1, 1) lob_i,
  3    instr ('Hello world', 'o', -1, 1) std_i,
  4    dbms_lob.getlength ('Hello world')
  5  	 - dbms_lob.instr (reverse ('Hello world'), 'o', 1, 1)
  6  	 + 1 rev_lob_i
  7  from dual;

     LOB_I      STD_I  REV_LOB_I
---------- ---------- ----------
                    8          8

1 row selected.





- Re: instr vs DBMS_LOB.instr [message #546968 is a reply to message #546967] Sat, 10 March 2012 15:55 Go to previous messageGo to next message
bigdataboy
Messages: 3
Registered: March 2012
Junior Member
@Michael: thank you for quick reply, it seems instr has same behaviour with backward direction as dbms_lob.instr.
@Barbara: Unfortunately reverse doesen't work with lobs (or with lobs longer than 4k char)
- Re: instr vs DBMS_LOB.instr [message #546992 is a reply to message #546968] Sun, 11 March 2012 00:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
reverse doesen't work with lobs (or with lobs longer than 4k char)


In addition it will work ONLY for ASCII characters (code point less than 128) and could return wrong result.
Remember REVERSE reverses the BYTES not the characters.

Regards
Michel
- Re: instr vs DBMS_LOB.instr [message #546995 is a reply to message #546992] Sun, 11 March 2012 01:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
it seems instr has same behaviour with backward direction as dbms_lob.instr


Are you sure?
SQL> l
  1  declare c clob; i integer;
  2  begin
  3    dbms_lob.CREATETEMPORARY (c, TRUE);
  4    c := rpad (' ', 4000) || rpad (' ', 4000) || rpad (' ', 4000) || 'Hello world';
  5    i := INSTR (c, 'o',-1,1) ;
  6    dbms_output.put_line(i);
  7* end;
SQL> /
12008

Regards
Michel
- Re: instr vs DBMS_LOB.instr [message #546999 is a reply to message #546995] Sun, 11 March 2012 03:33 Go to previous message
bigdataboy
Messages: 3
Registered: March 2012
Junior Member
@Michael: thank you so much for REPLACE specification, prevented me from many mistakes (in fact I'm handling charsets not in ASCII).

About instr on lobs: I confirm you statement, it works even backwards, thanks.

I keep on having errors about that, but at this point I guess it's caused by the exact lob I'm handling and the exact pattern I'm looking for (pretty random utf-8 objects).

Thank you everyone for your time and suggestions, it really did help in clearing out some stuff.
Previous Topic: Collections
Next Topic: Cursor variables? (merged 3)
Goto Forum:
  


Current Time: Sat May 31 06:20:47 CDT 2025