1

I need a function or procedure in SQL Server 2014 that can read the content between (<?>) the sign tags for this example.

Thanks.

<ul>how are you?</ul>

Output1: should be ul or /ul

Is this possible?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohsen Nz
  • 25
  • 6
  • Is this the extent of the text? If these tags be buried in a larger HTML document, then should look for a parser. – Tim Biegeleisen Sep 10 '16 at 06:10
  • You could cast the input as XML and use something like the answer here: http://stackoverflow.com/a/2274091/5552667 (depending on your needs). – ZLK Sep 10 '16 at 06:38
  • MR.tim , HTML document.help me create func or procedure for Parser . – Mohsen Nz Sep 10 '16 at 07:00

2 Answers2

0

If you requirement is too simple to just read content between a tag then you can use the following logic:

declare @str varchar(200) = '<ul>how are you?</ul>'
select SUBSTRING(@str, CHARINDEX('<', @str) + 1, CHARINDEX('>', @str) - CHARINDEX('<', @str) -1)

However, this will not work if you have multiple tags in the same string. If you need to handle more complex scenario you can use xquery. I think this link might help you to do so: xquery to return the element name

Community
  • 1
  • 1
Samrat Alamgir
  • 355
  • 2
  • 13
0

You can use local-name XPath function:

DECLARE @x xml = '<ul>how are you?</ul>'

SELECT  s.d.value('local-name(.)','nvarchar(100)') as TagName,
        s.d.value('.','nvarchar(100)') as SomeValue
FROM @x.nodes('/*') as s(d)

Output:

TagName SomeValue
ul      how are you?
gofr1
  • 15,741
  • 11
  • 42
  • 52