Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract a substring from clob in oracle

I have a clob with data

<?xml version='1.0' encoding='UTF-8'?><root available-locales="en_US" default-locale="en_US"><static-content language-id="en_US"><![CDATA[<script type="text/javascript">
function change_case()
{
    alert("here...");
    document.form1.type.value=document.form1.type.value.toLowerCase();
}
</script>

<form name=form1 method=post action=''''>
<input type=text name=type value=''Enter USER ID'' onBlur="change_case();">
<input type=submit value=Submit> </form>
</form>]]></static-content></root>

I want to extract the line with the onblur attribute, in this case:

<input type=text name=type value=''Enter USER ID'' onblur="change_case();">
like image 937
micheal marquiz Avatar asked Apr 12 '12 18:04

micheal marquiz


People also ask

Can we use Substr on CLOB in Oracle?

SUBSTR and REGEXP_SUBSTR work on CLOBs as well.

How extract part of a string in Oracle?

Use a SUBSTR() function. The first argument is the string or the column name. The second argument is the index of the character at which the substring should begin. The third argument is the length of the substring.

Can we convert CLOB to string?

To convert CLOB data type to string Reader r = clob. getCharacterStream(); Read each character one by one from the retrieved Stream of characters and append them to the StringBuilder or StringBuffer.

How do I find a specific word in a string in Oracle?

The Oracle INSTR function is used to search string for substring and find the location of the substring in the string. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring.


2 Answers

Tom Kyte say how get varchar2 from clob in SQL or PL/SQL code

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:367980988799

And when you have varchar2 you can use SUBSTR or REGEXP_SUBSTR function for extract the line.

http://docs.oracle.com/cd/B14117_01/server.101/b10759/functions147.htm#i87066

http://docs.oracle.com/cd/B14117_01/server.101/b10759/functions116.htm

If you want to use SQL code, you can create this request

select col1, col2, func1(dbms_lob.substr( t.col_clob, 4000, 1 )) from table1 t

And in PL/SQL function "func1" you can do what you want with input string using SUBSTR or any other functions

like image 124
Alexander Avatar answered Oct 01 '22 11:10

Alexander


Subdivide your problem. You want to extract a line of text from your CLOB which contains a particular substring. I can think of two possible interpretations of your requirements:

Option 1.

  1. Split the CLOB into a series of lines - e.g. split it by newline/carriage return characters if that's really what you meant by "line".
  2. Check each line to see if it includes the substring, e.g. onblur. If it does, you have found your line.

Option 2.

If you don't actually mean the line, but you want the <script>...</script> html fragment, you can use similar logic:

  1. Search for the first occurrence of <script>.
  2. Search for the next occurrence of </script> after that point. Extract the substring from <script> to </script>.
  3. Search the substring for onblur. If it is found, return the substring. Otherwise, find the next occurrence of <script>, go to step 2, rinse, repeat.
like image 31
Jeffrey Kemp Avatar answered Oct 01 '22 12:10

Jeffrey Kemp