Solved

Remove first 5 and last 6 characters of string

Posted on 2008-10-23
5
2,615 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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