I have certain values in the table with different XML Structures.
There is a tag <problemDesc> 'error' </problemDesc>
which is present in all the XML Blocks.
How can I extract the error from the tag by using SubStr and InStr functions? or is there any way out to get the value.
Using string function to parse XML is not good idea. Use built-in XML parsing capabilities instead:
select *
FROM
XMLTABLE('//problemDesc'
PASSING
xmltype('<problemDesc>error</problemDesc>')
COLUMNS
error varchar2 (200) PATH '.'
) xmlt
;
Although I agree this is a bad idea, to answer the question in the way you asked something like this would work:
SELECT SUBSTR('<problemDesc>Error 1</problemDesc>',
INSTR('<problemDesc>Error 1</problemDesc>', '<problemDesc>') + LENGTH('<problemDesc>'),
INSTR('<problemDesc>Error 1</problemDesc>', '</problemDesc>') - (INSTR('<problemDesc>Error 1</problemDesc>', '<problemDesc>') + LENGTH('<problemDesc>')))
FROM DUAL;
in other words
SELECT SUBSTR(COL_NAME,
INSTR(COL_NAME, '<problemDesc>') + LENGTH('<problemDesc>'),
INSTR(COL_NAME, '</problemDesc>') - (INSTR(COL_NAME, '<problemDesc>') + LENGTH('<problemDesc>')))
FROM SOME_TABLE;
This only works as long as these tags don't appear multiple times. But, in theory you are looking for the start of the START tag, and want to go to the beginning of the END tag.
why string manipulation ?? try this :
SQL> select extractvalue(xmltype('<a>abhi</a>'),'//a/text()') from dual;
EXTRACTVALUE(XMLTYPE('<A>ABHI<
--------------------------------------------------------------------------------
abhi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.