0

I have a variable in a stored procedure that contains a string of characters like

[Tag]MESSAGE[/Tag]

I need a way to get the MESSAGE part from within the tags. Any help would be much appreciated

jarlh
  • 42,561
  • 8
  • 45
  • 63
Eagz
  • 21
  • 4

3 Answers3

1

Note: I have tested it on Oracle RDBMS

A more reliable approach is to use REGEXP_REPLACE.

REGEXP_REPLACE(value, pattern)

Example

SELECT  REGEXP_REPLACE(
        '<Tag>Message</Tag>',
         '\s*</?\w+((\s+\w+(\s*=\s*(".*?"|''.*?''|[^''">\s]+))?)+\s*|\s*)/?>\s*') FROM DUAL;
         

Just replace "<" with "[" if your tags are different

Yousha Arif
  • 1,188
  • 8
  • 20
0

What you need is this:

SELECT SUBSTRING(ColumnName,CHARINDEX('html_tag',ColumnName)+LEN('html_tag'),CHARINDEX('html_close_tag',ColumnName)-LEN('html_close_tag')) FROM TableName

You'll require to change the html_tag and html_close_tag with your own HTML tag that you want to get rid of.

Vishal A.
  • 1,373
  • 8
  • 19
0

If the column contains only single tag, simple call of substring function should be enough. Otherwise there will always be some point where regular expression does not suffice since you fall into trap (see this legendary StackOverflow answer).

Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64