?
Solved

Remove first 5 and last 6 characters of string

Posted on 2008-10-23
5
Medium Priority
?
2,617 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 2000 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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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