Solved

Remove first 5 and last 6 characters of string

Posted on 2008-10-23
5
2,616 Views
Last Modified: 2012-05-05
I need to write a stored procedure that will check the first 5 and last 6 charcters of a string.  If the first 5 characters = '<div>' and the last 6 = '</div>' i need to remove them from the string.  I need to loop this to remove multiple instances. i.e. if the string was '<div><div>some stuff here</div></div>' the result would be 'some stuff here'
0
Comment
Question by:APhoenixRising
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22784364
declare @s as varchar(max)
set @s ='<div><div>some stuff here</div></div>'
set @s= replace(replace(@s,'<div>',''),'</div>','')
select @s
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22784371
if u r using MS SQL 2000 , change  varchar(max) to varchar(1000)
0
 

Author Comment

by:APhoenixRising
ID: 22784382
Hi, this won't work as it will replace all occurences of <div> or </div>.  i only want to remove them if they are the first and last instances in the string.  i.e. if the string was: '<div><div>some <div> stuff </div>here</div></div>'
it would return: some <div> stuff </div>here
0
 
LVL 14

Accepted Solution

by:
Binuth earned 500 total points
ID: 22784420

declare @s as varchar(1000)
set @s ='<div><div>some stuff <div> here</div></div>'
 
while (left(@s,5)='<div>' or right(@s,6)='</div>')
begin
	if(left(@s,5)='<div>')
		set @s = substring(@s,6,len(@s)-5)
	else
		set @s = substring(@s,1,len(@s)-6)
end
select @s

Open in new window

0
 

Author Closing Comment

by:APhoenixRising
ID: 31509135
Fantastic.  Many thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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