Sunday, October 31, 2010

TSQL Beginners Challenge 18 - Count the total occurrences of HTML tags in the given string

This challenge involves counting the number of occurrences of HTML tags in a given list of strings. You can assume that there will be only valid HTML tags in the input strings. The output should display tags in ascending order.
Sample Input Data
ID  HtmlText
-- -------------------------------------------------------------------------
1   <Html><Body><Font>This is challenge #18</Font><Font></Font></Body></Html>

Expected Output
ID  TagNamesOccurance
--  -----------------------------------------------------------------
1   Body(Found: 1 time), Font(Found: 2 times), Html(Found: 1 time)
Script
DECLARE @t TABLE(ID INT IDENTITY, HtmlText VARCHAR(Max))
INSERT INTO @t(HtmlText)
SELECT '<Html><Body><Font>This is challenge #18</Font><Font></Font></Body></Html>'
SELECT * FROM @t

Read more: Beyond Relational