Solved

Remove first 5 and last 6 characters of string

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle - Query Insert and Update multiple tables 5 57
Query syntax 10 40
SQL join help to a thrid table 51 75
Help Extract Specific in SQL 8 16
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now