like this
Select substring(htmlfield,pos1,c
from (
select charindex('<BR>',htmlfield
from #mytable
) as y
Main Topics
Browse All TopicsIn 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-c
but i dont think this will work when i use it from excel.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
your query
Select case when htmlfield like '%<BR>%<BR>%<BR>%<BR>%' then substring(htmlfield,pos1,c
from (
select charindex('<BR>',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.
sorry for delay
select charindex('<BR>',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
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
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,c
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,c
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
Business Accounts
Answer for Membership
by: sajuksPosted on 2006-05-11 at 22:36:07ID: 16664980
And please no links only a valid and working suggestion.