Usage of dbms_lob.substr()

Hi all,
I have a question to the usage of dbms_lob.substr function in PL/SQL.
I have a clob with dbms_lob.getlength(l_clob) = 12295.
When I call dbms_lob.substr to get the clob starting from the specified location, I miss
data at the end of the result clob.
declare
   l_clob         clob;
   l_result_clob  clob;
   l_len          number;
begin
   l_len := dbms_lob.getlength(l_clob);         -- len => 12295
   l_result_clob := dbms_lob.substr(l_clob, 12239, 56);
   l_len := dbms_lob.getlength(l_result_clob);  -- len => 10958
end;
/With a smaller clob there are no such problems.
Any Idea what happend?
Thanks
Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
NLS_CHARACTERSET        AL32UTF8
NLS_NCHAR_CHARACTERSET  AL16UTF16

Thank you very much for this quick and exact response.
DBMS_LOB.READ worked fine.
Unfortunately I can find no information about this bug - can you help me with that?
Thanks once again!
Cheers

Similar Messages

  • Error while converting CLOB to varchar using DBMS_LOB.SUBSTR() in Oracle11g

    Hi
    Whenever I am using DBMS_LOB.SUBSTR(columnname,4000,1) package for a clob column in a simple Select Query, the following error is thrown for Oracle 11g version.
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 1.
    Please find the installation details of the database and the character set
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE     11.2.0.1.0     Production
    TNS for Linux: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    PARAMETER VALUE
    NLS_LANGUAGE AMERICAN
    NLS_TERRITORY AMERICA
    NLS_CURRENCY $
    NLS_ISO_CURRENCY AMERICA
    NLS_NUMERIC_CHARACTERS .,
    NLS_CHARACTERSET AL32UTF8
    NLS_CALENDAR GREGORIAN
    NLS_DATE_FORMAT DD-MON-RR
    NLS_DATE_LANGUAGE AMERICAN
    NLS_SORT BINARY
    NLS_TIME_FORMAT HH.MI.SSXFF AM
    NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
    NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
    NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
    NLS_DUAL_CURRENCY $
    NLS_COMP BINARY
    NLS_LENGTH_SEMANTICS BYTE
    NLS_NCHAR_CONV_EXCP FALSE
    NLS_NCHAR_CHARACTERSET AL16UTF16
    NLS_RDBMS_VERSION 11.2.0.1.0
    It is working fine if I reduce from 4000 to 3500 . But I want to use 4000.
    Please let me know if any solution

    you are using a multibyte character set:
    NLS_CHARACTERSET AL32UTF8
    so each character takes between 1 and 4 bytes of storage.
    a varchar2(4000) column can only hold 4000 BYTES. Regardless of the character set - varchar2 is limited to 4000 bytes.
    In a single byte character set, that is 4000 characters as a character = a byte
    In your character set, a varchar2(4000) can hold somewhere between 1000 and 4000 characters - depending on what the characters are.
    So, you must have some CLOB whose first 4000 characters include at least one "more than one byte" character. That won't fit into a varchar2(4000)
    Your approach of backing off the substr size is reasonable (and you'll need to remember that in your application - end users can type in as little as 1000 characters and get an error about the column being too small!) as it reduces the number of BYTES to be belong 4000.

  • Oracle error while using function dbms_lob.substr()

    Following sql statement is causing error select dbms_lob.substr(clob_colum,32767) from Table* when the size of clob_column goes above 4 KB. The error message is given below
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at line 1
    The issue is getting resolved when reducing the size of the CLOB column.
    Is this a limitation of oracle query? Can anybody please help me to resolve this error? Thanks in Advance.
    Regards,
    Shine

    Hi.
    >
    We are using this query inside a package and the data is used to generate an XML file and it finally used to generate a pdf report. There exists many clob records with huge size and in that case report execution fails with the oracle message.
    >
    While the XML file is generated or PDF report?
    Which message? ORA-06502: PL/SQL: numeric or value error: character string buffer too small ???
    >
    So we need to modify the query in a way that it executes fine even if the size is more than 4 KB(upto a possible size). I tried using substr function against the clob column but it does not returned any value.
    >
    Then, do you need the first 4 KB or the entire CLOB?
    Can't you write CLOB directly?
    Please post some example data as well as your desired output in order to understand completely your problem and test our ideas.
    Regards.

  • Dbms_lob.substr returns varchar2?????

    I have a 400kb text string in a clob variable. I need to strip out certain characters to remove html tags. To do this I need to do lots of substr
    This is taking a very long time and using massive amounts of temp tablespace, nearly 2GB!!
    I'm trying to use dbms_lob.substr instread as I assume the issue is there are lots of clob to varchar2 conversions going on or something, still seems excessive that a 400kb file can consume nearly 2GB of temp tablespace!!
    When I use dbms_lob.substr on the clob you cant return more than 32767 characters as it returns a varchar2, I need to to return a clob ... am I missing something as it seems ridiculous that dbms_lob.substr does not return a clob
    My only work around is to have a varchar2(32767) array, break my clob down into chunks and, work with that and then stitch it all back together into a clob again ...... I'm hoping I am missing something and there is a better way
    Thanks
    Robert

    At the moment I am just using substr, which does the job but takes a long time (over 20 minutes) and makes massive use of the temp tablespace
    I calculated the size by using our test database with no users connected, made a single temp datafile of 500MB (which can auto extend) and when the routine had finished that file was 1.8GB.
    I re-ran the routine again to make sure the time was not taken up resizing the temp tablespace and it took the same time.

  • DBMS_LOB.SUBSTR() returning 8191 characters instead of 32767

    Hi all,
    I just run into an interesting issue.
    Trying to convert a CLOB to VARCHAR2(32767) I only got 8191 characters.
    Here is a code sample:
    DECLARE
    v_clob clob := '';
    v_vchar VARCHAR2(32767);
    j INTEGER := 0;
    BEGIN
    FOR i IN 1..32767 LOOP
    v_clob := v_clob || to_char(j);
    j := j + 1;
    IF j = 10 THEN
    j := 0;
    END IF;
    END LOOP;
    v_vchar := dbms_lob.substr(v_clob, 32767, 1);
    dbms_output.put_line('Length: ' || length(v_vchar));
    dbms_output.put_line('First: ' || substr(v_vchar, 1, 1));
    dbms_output.put_line('Last: ' || substr(v_vchar, length(v_vchar), 1));
    END;
    Looking at this one should expect the length to be 32767, the first character in the v_vchar variable - 0 and the last one - 6, right? Unfortunately here is the output I get:
    Length: 8191
    First: 0
    Last: 0
    Running trough some google searches I only found a couple of posts regarding the issue and no reasonable explanation or fix, workaround or something. A guy had the same problem and his conclusion was that it was a platform dependent issue - he got the same result using DBMS_LOB.SUBSTR() on Oracle running on Solaris and the correct result running on a Windows Server.
    Reading those few posts made me think that's a bug.
    My platform - Oracle 10g running on Linux. I haven't tried it on a Windows (that's not my point).
    Any help / ideas / explanations would be highly appreciated! :-)

    Thank you very much for this quick and exact response.
    DBMS_LOB.READ worked fine.
    Unfortunately I can find no information about this bug - can you help me with that?
    Thanks once again!
    Cheers

  • Dbms_lob.substr ( difference with  simple function substr)

    Hello  suppose the following query
    SELECT employee_id, resume,
                  DBMS_LOB.SUBSTR (resume, 5, 18)  lob_substr,
                  substr(resume, 5, 18)   sub   
    FROM employees
    WHERE employee_id = 170;
    resume is of CLOB datatype
    and for employee_id 170 is 'Date of Birth: 1 June 1956 Resigned: 30 September 2000'
    the results are:
    sub
    ' of Birth: 1 June '
    I understand that.
    I cannot understand why  DBMS_LOB.SUBSTR (resume, 5, 18)  gives as a result
    'June '
    Thank you.
    What is the difference?

    Sorry, I found the answer myself.
    dms_lob.substr is different from function substr
    DBMS_LOB.SUBSTR(lob_column, no_of_chars, starting)
    no_of_chars : how many characters it will return, so in my example, 5 characters will be returned, that's why 'June '
    starting from position 18

  • DBMS_LOB.Substr Issues

    Hello everyone,
    I'm trying to use dbms_lob.substr on a clob column in one of my tables. I'm searching the table for a particular phrase, which should be the first two words in this column. Any row that has that phrase should be returned. However, when I run the script, I get only rows where the ONLY words in the entire column are the two words. Here's an example
    | ID | | Name | | Information |
    | 1 | | Jorge | | Terminated for negligence |
    | 2 | | Stephanie | | Terminated for ... ? |
    I need the script to return both of those rows.
    Here's my script:
    WHERE
    dbms_loc.substr("EMPLOYEE"."COMMENTS", 15, 1)='Terminated for'
    Thanks,
    Joe

    Nevermind, fixed it by adding a space to the end of the search string.
    They really need a delete feature up here.

  • Parameters for substr and dbms_lob.substr

    The syntax for substr in SQL functions and for the function in dbms_lob package is not matching and hence confusing for programmers. It will be less confusing if parameters for both functions are of the same order.
    In Sql functions the the syntax for substr is substr(string, offset, amount)
    In dbms_lob package the syntax for the substr function is dbms_lob.substr(lob_loc, amount, offset)
    Mohan

    I think he wants to send a suggestion to Oracle via this forum.

  • DBMS_LOB.substr

    When I run the following query to select the first 4001 characters from the CLOB field:
    select dbms_lob.substr(field,4001,1)
    from table
    I get the following error:
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    If I change it to 4000 or less it works fine.
    Alan
    null

    Hello,
    It is possible that you have to use a select into statement and work further wit the result.
    Here is an example I found in Oracle documentation :
    PROCEDURE Example_14a IS
    src_lob CLOB;
    pos INTEGER := 2147483647;
    buf VARCHAR2(32000);
    BEGIN
    SELECT c_lob INTO src_lob FROM lob_table
    WHERE key_value = 21;
    buf := DBMS_LOB.SUBSTR(src_lob, 32767, pos);
    /* process the data */
    END;
    Hope this helps you.
    Regards,
    Cindy
    null

  • Performance diff between substr() and dbms_lob.substr()?

    I have a ton of existing views that use dbms_lob.substr(MyClobColumn).
    A developer recently noticed he got the same results with substr(MyClobColumn) - 10.1.0.3. Must have been added functionality on a patch/upgrade somewhere.
    Is there a performance gain by dropping the "dbms_lob."?
    Ken

    From the manual.
    The SUBSTR functions return a portion of string, beginning at character position, substring_length characters long. SUBSTR calculates lengths using characters as defined by the input character set. SUBSTRB uses bytes instead of characters. SUBSTRC uses Unicode complete characters. SUBSTR2 uses UCS2 code points. SUBSTR4 uses UCS4 code points.
    If position is 0, then it is treated as 1.
    If position is positive, then Oracle Database counts from the beginning of string to find the first character.
    If position is negative, then Oracle counts backward from the end of string.
    If substring_length is omitted, then Oracle returns all characters to the end of string. If substring_length is less than 1, then Oracle returns null.
    string can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. Both position and substring_length must be of datatype NUMBER, or any datatype that can be implicitly converted to NUMBER, and must resolve to an integer. The return value is the same datatype as string. Floating-point numbers passed as arguments to SUBSTR are automatically converted to integers.

  • Problem with DBMS_LOB.SUBSTR

    Hi,
    i want to encode a BLOB into BASE64_ENCODE.
    Here my PL/SQL function:
    FUNCTION encodeBlob2Base64(pBlobIn IN BLOB)
    RETURN BLOB
    IS
    vAmount PLS_INTEGER := 20000;
    vBase64Blob BLOB;
    vBlobIn BLOB;
    vOffset PLS_INTEGER := 1;
    BEGIN
    vBlobIn := pBlobIn;
    dbms_lob.createtemporary(lob_loc => vBase64Blob, CACHE => FALSE);
    LOOP
    dbms_lob.append(dest_lob => vBase64Blob,
    src_lob => utl_encode.base64_encode(r =>
    dbms_lob.substr(lob_loc => vBlobIn, amount => vAmount, offset => vOffset)));
    vOffset := vOffset + vAmount;
    END LOOP;
    dbms_lob.freetemporary(lob_loc => vBase64Blob);
    RETURN vBase64Blob;
    EXCEPTION
    WHEN OTHERS THEN
    RAISE;
    END encodeBlob2Base64;
    When i now use my function i get the ORA-29261: bad argument in the statement DBMS_LOB.SUBSTR(lob_loc ...)
    Has somebody an idea, what's wrong?
    Many thanks in advance.
    Regards,
    Martin

    The reason is that utl_encode returns a raw whereas dbms_lob.append expects either a blob or a clob.
    SQL> desc utl_encode;
    FUNCTION BASE64_DECODE RETURNS RAW
    Argument Name                  Type                    In/Out Default?
    R                              RAW                     IN   
    FUNCTION BASE64_ENCODE RETURNS RAW
    Argument Name                  Type                    In/Out Default?
    R                              RAW                     IN   
    FUNCTION MIMEHEADER_DECODE RETURNS VARCHAR2
    Argument Name                  Type                    In/Out Default?
    BUF                            VARCHAR2                IN   
    FUNCTION MIMEHEADER_ENCODE RETURNS VARCHAR2
    Argument Name                  Type                    In/Out Default?
    BUF                            VARCHAR2                IN   
    ENCODE_CHARSET                 VARCHAR2                IN     DEFAULT
    ENCODING                       BINARY_INTEGER          IN     DEFAULT
    FUNCTION QUOTED_PRINTABLE_DECODE RETURNS RAW
    Argument Name                  Type                    In/Out Default?
    R                              RAW                     IN   
    FUNCTION QUOTED_PRINTABLE_ENCODE RETURNS RAW
    Argument Name                  Type                    In/Out Default?
    R                              RAW                     IN   
    FUNCTION TEXT_DECODE RETURNS VARCHAR2
    Argument Name                  Type                    In/Out Default?
    BUF                            VARCHAR2                IN   
    ENCODE_CHARSET                 VARCHAR2                IN     DEFAULT
    ENCODING                       BINARY_INTEGER          IN     DEFAULT
    FUNCTION TEXT_ENCODE RETURNS VARCHAR2
    Argument Name                  Type                    In/Out Default?
    BUF                            VARCHAR2                IN   
    ENCODE_CHARSET                 VARCHAR2                IN     DEFAULT
    ENCODING                       BINARY_INTEGER          IN     DEFAULT
    FUNCTION UUDECODE RETURNS RAW
    Argument Name                  Type                    In/Out Default?
    R                              RAW                     IN   
    FUNCTION UUENCODE RETURNS RAW
    Argument Name                  Type                    In/Out Default?
    R                              RAW                     IN   
    TYPE                           BINARY_INTEGER          IN     DEFAULT
    FILENAME                       VARCHAR2                IN     DEFAULT
    PERMISSION                     VARCHAR2                IN     DEFAULT
    SQL> spool off

  • Dbms_lob.substr is failing with ORA-06502

    The following query is throwing error while doing minus with dbms_lob.substr although 4000
    characters are being selected. Column billing_inventory is of CLOB. Please advice.
    SQL> select count(1) from llums_feed;
    724754
    SQL> select count(1) from llums_feed_prv;
    0
    SQL> SELECT
    asset_id,dbms_lob.substr(billing_inventory,4000,1),dbms_lob.substr(billing_inventory,8000,4001)
    FROM llums_feed
    minus
    select asset_id, dbms_lob.substr(billing_inventory,4000,1)
    ,dbms_lob.substr(billing_inventory,8000,4001)
    from llums_feed_prv;
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at line 1

    Hello
    You are selecting 8000 characters from position 4001, not 4000 characters from position 4001....
      FUNCTION substr(lob_loc IN BLOB,
                      amount  IN INTEGER := 32767,
                      offset  IN INTEGER := 1)dbms_lob.substr(billing_inventory,4000,1),dbms_lob.substr(billing_inventory,8000,4001)
    You need to modify the 2nd substr in each select statement to be
    dbms_lob.substr(billing_inventory,4000,1),dbms_lob.substr(billing_inventory,4001,4000)
    HTH
    David
    Edited by: David Tyler on Jun 27, 2011 1:16 PM

  • DBMS_LOB.SUBSTR with NCLOB data type

    Hi ,
    When I am trying to extract part of the string from Comments column(NCLOB datatype) am facing issue.
    ORA-06502:PL/SQL Numeric or value error :Character string buffer too small
    select dbms_lob.substr(a.COMMENTS, 4000, 1),
    from VW_DETAILS a
    where a.id = 6210872
    DBMS_LOB.SUBSTR with NCLOB data type
    Table structure :
    Column_name Data_type
    Comments NCLOB
    ID NUMBER
    Regards,
    Venkat

    DBMS_LOB.SUBSTR parameter amount for CLOB/NCLOB indicates number of characters to substring, not bytes. And 4000 multi-byte characters (since you are using NCLOB) is greater than 4000 bytes which is absolute limit for VARCHAR2 in SQL.
    SY.

  • Failed to convert Clob to String by using dbms_lob.substr

    it says that the cache is not enough to do this.
    I would like to get some infos from two tables as a View.
    and one of the table contains a Clob data which is large
    than 4k i think.
    and when I tried to create the view, it says that expecting
    - but got Clob, so I would like to change it to String by using
    dbmas_lob.substr function and met the cache limited problem.
    The database I am using is 10.2.0.1.0.

    Thanks for reply.
    and the problem was fixed.
    after I removed 'distinct', which specified after 'select'.

  • Bug 10395395: ORA-57000: TT8507: ORA-00600 ON DBMS_LOB.SUBSTR()

    Bug# 10395395 is not published. Can you telll me if it is platform specific?
    Thanks.

    Open an SR at MyOracleSupport.

Maybe you are looking for