How to query a CLOB column in Oracle

前端 未结 8 1445
鱼传尺愫
鱼传尺愫 2020-11-27 06:24

I\'m trying to run a query that has a few columns that are a CLOB datatype. If i run the query like normal, all of those fields just have (CLOB) as the value.

相关标签:
8条回答
  • 2020-11-27 06:28

    For big CLOB selects also can be used:

    SELECT dbms_lob.substr( column_name, dbms_lob.getlength(column_name), 1) FROM foo

    0 讨论(0)
  • 2020-11-27 06:46

    When getting the substring of a CLOB column and using a query tool that has size/buffer restrictions sometimes you would need to set the BUFFER to a larger size. For example while using SQL Plus use the SET BUFFER 10000 to set it to 10000 as the default is 4000.

    Running the DBMS_LOB.substr command you can also specify the amount of characters you want to return and the offset from which. So using DBMS_LOB.substr(column, 3000) might restrict it to a small enough amount for the buffer.

    See oracle documentation for more info on the substr command

    
        DBMS_LOB.SUBSTR (
           lob_loc     IN    CLOB   CHARACTER SET ANY_CS,
           amount      IN    INTEGER := 32767,
           offset      IN    INTEGER := 1)
          RETURN VARCHAR2 CHARACTER SET lob_loc%CHARSET;
    
    
    0 讨论(0)
  • 2020-11-27 06:47

    I did run into another condition with HugeClob in my Oracle database. The dbms_lob.substr only allowed a value of 4000 in the function, ex:

    dbms_lob.substr(column,4000,1)
    

    so for my HughClob which was larger, I had to use two calls in select:

    select dbms_lob.substr(column,4000,1) part1, 
           dbms_lob.substr(column,4000,4001) part2 from .....
    

    I was calling from a Java app so I simply concatenated part1 and part2 and sent as a email.

    0 讨论(0)
  • 2020-11-27 06:47

    To add to the answer.

    declare
    v_result clob;
    begin
    ---- some operation on v_result
    dbms_lob.substr( v_result, 4000 ,length(v_result) - 3999 );
    
    end;
    /
    

    In dbms_lob.substr

    first parameter is clob which you want to extract .

    Second parameter is how much length of clob you want to extract.

    Third parameter is from which word you want to extract .

    In above example i know my clob size is more than 50000 , so i want last 4000 character .

    0 讨论(0)
  • 2020-11-27 06:48

    If it's a CLOB why can't we to_char the column and then search normally ?

    Create a table

    CREATE TABLE MY_TABLE(Id integer PRIMARY KEY, Name varchar2(20), message clob);
    

    Create few records in this table

    INSERT INTO MY_TABLE VALUES(1,'Tom','Hi This is Row one');
    INSERT INTO MY_TABLE VALUES(2,'Lucy', 'Hi This is Row two');
    INSERT INTO MY_TABLE VALUES(3,'Frank', 'Hi This is Row three');
    INSERT INTO MY_TABLE VALUES(4,'Jane', 'Hi This is Row four');
    INSERT INTO MY_TABLE VALUES(5,'Robert', 'Hi This is Row five');
    COMMIT;
    

    Search in the clob column

    SELECT * FROM MY_TABLE where to_char(message) like '%e%';
    

    Results

    ID   NAME    MESSAGE   
    ===============================  
    1    Tom     Hi This is Row one         
    3    Frank   Hi This is Row three
    5    Robert  Hi This is Row five
    
    0 讨论(0)
  • 2020-11-27 06:49

    This works

    select DBMS_LOB.substr(myColumn, 3000) from myTable
    
    0 讨论(0)
提交回复
热议问题