Solved

Remove first 5 and last 6 characters of string

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

11 Experts available now in Live!

Get 1:1 Help Now