Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Strip Html tags

Posted on 2006-05-11
9
Medium Priority
?
2,095 Views
Last Modified: 2008-01-09
In a field(say htmlfield varchar(200)) in the table(mytable)  i have some data in the format shown below.

<b>Label A</b><br>Some Label<br><b>Label B</b><br>Need This value<br><b>End Look</b><br>The value - between this is what i want<br>

i need a query to extract the value between the second  br tags thats
"Need This value"
and the third br tags that
"The value - between this is what i want."

What am actaully trying to achieve is to display only the relevant data to the users in the excel sheet ( Am using the new database query option available in excel to connect to SQL Server 2000).
I cannot change the way the data is inserted. Neither can i do any datatype changes.

Thanks for any help.

I have seen http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=197&lngWId=5
but i dont think this will work when i use it from excel.
0
Comment
Question by:sajuks
  • 5
  • 4
9 Comments
 
LVL 33

Author Comment

by:sajuks
ID: 16664980
And please no links only a valid and working suggestion.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16665406
like this

Select substring(htmlfield,pos1,charindex('<BR>',htmlfield,pos1)-pos1)
  from (

   select charindex('<BR>',htmlfield,charindex('<BR>',htmlfield,charindex('<BR>',htmlfield)+4)+4) + 4 as pos1,htmlfield
   from #mytable

) as y
0
 
LVL 33

Author Comment

by:sajuks
ID: 16665443
am getting the error
Invalid length parameter passed to the substring function.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16667031
Select case when htmlfield like '%<BR>%<BR>%<BR>%<BR>%' then substring(htmlfield,pos1,charindex('<BR>',htmlfield,pos1)-pos1) else HTMLFIELD END
  from (

   select charindex('<BR>',htmlfield,charindex('<BR>',htmlfield,charindex('<BR>',htmlfield)+4)+4) + 4 as pos1,htmlfield
   from #mytable

) as y
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16669282
does all the data have at least 3 sets of <BR>?

what do you want to happen when it doesn't?

is <BR>   valid without a <\BR>?
(my solution would change slightly if a <\BR> was valid)

hth

 
0
 
LVL 33

Author Comment

by:sajuks
ID: 16672574
your query
Select case when htmlfield like '%<BR>%<BR>%<BR>%<BR>%' then substring(htmlfield,pos1,charindex('<BR>',htmlfield,pos1)-pos1) else HTMLFIELD END
  from (

   select charindex('<BR>',htmlfield,charindex('<BR>',htmlfield,charindex('<BR>',htmlfield)+4)+4) + 4 as pos1,htmlfield
   from #mytable

) as y

works perfectly in the respect that it retireves the first set of data that i want,
How do i get the second part of the data. ??
So in the first example that i gave it is able to retrieve  "Need This value"
now i want the data  "The value - between this is what i want."

>>>does all the data have at least 3 sets of <BR>?
yes that validation occurs so there will always be three sets of that

>>is <BR>   valid without a <\BR>? (my solution would change slightly if a <\BR> was valid)
You can take the format that i gave in my first post as final. There will be no changes in that.

Sorry for not replying earlier but no net access. Many thanks for your help till now.


0
 
LVL 33

Author Comment

by:sajuks
ID: 16680297
Lowfatspread, did u get any time to solve the query that i'd posted. ? Thanks for your time and my apologies for intruding into ur work.
0
 
LVL 33

Author Comment

by:sajuks
ID: 16680649
I solved the query using your example...but  can you help me in understanding what you've written in ENglish/psuedo code.
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 16688849
sorry for delay

 select charindex('<BR>',htmlfield,charindex('<BR>',htmlfield,charindex('<BR>',htmlfield)+4)+4) + 4 as pos1,htmlfield
   from #mytable


the charindex function looks for the first parameter within the second parameter and has an optional 3 parameter which specifies the
position in the second parameter at which to start the search...

so
charindex('<BR>',Htmlfield)  
searches Htmlfield for a <BR> and returns the position of the first occurrence

and charindex('<BR>',HTMLFIELD,charindex('<BR>',HTMLFIELD)+4)

the nested charindex returns the position at which the first '<BR>' is found
since '<BR>' is 4 characters long
we add 4 to that position
and thats where the outer charindex begins its search for a '<BR>'
and returns the position of the second '<BR>' in the HTMLFIELD column

so
charindex('<BR>',htmlfield,charindex('<BR>',htmlfield,charindex('<BR>',htmlfield)+4)+4) + 4
finds the third <BR>  and adds 4 to give the start position of theText in the 3 set of <BR>'s in HTMLFIELD



the outer query
Select case when htmlfield like '%<BR>%<BR>%<BR>%<BR>%' then substring(htmlfield,pos1,charindex('<BR>',htmlfield,pos1)-pos1) else HTMLFIELD END
  from (


the LIKE Clause confirms that at least 4 <BR>'s  are present in HTMLFIELD
so that the SUBSTRING  can use the position of the fourth <BR> to determine the length of the text between the  3rd and 4th <BR>'s

SO  substring(htmlfield,pos1,charindex('<BR>',htmlfield,pos1)-pos1)

gets the text starting at POS1 which was determined in the inner subquery  
and then uses charindex to calculate the position of the 4th <BR> in HTLMFIELD (by starting at the pos1 position)
the subtraction of pos1 from this position give the length of the text

hth
 



0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question