Connecting Tech Pros Worldwide Help | Site Map

SQL Replace in Replace

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 23rd, 2007, 12:06 PM
Newbie
 
Join Date: Jan 2007
Posts: 2
Default SQL Replace in Replace

Hi

I'm looking for a way to get a substring using replace. I've heard that this may be possible by using a kind of nested replace.
My problem is that I have a text like

<font size=3>I love my job</font>

and I want to end up with,

I love my job

Thank you
Reply
  #2  
Old January 23rd, 2007, 03:35 PM
iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
Default

[PHP]declare @sHTML varchar(50)
set @sHTML = '<font size=3>I love my job</font>'
select substring(@sHTML,charindex('<font size=3>',@sHTML) + len('<font size=3>'),charindex('</font>',@sHTML) - len('<font size=3>') -1)[/PHP]
Reply
  #3  
Old January 23rd, 2007, 04:19 PM
almaz's Avatar
Expert
 
Join Date: Dec 2006
Location: Kyiv, Ukraine
Age: 28
Posts: 167
Default

1. What version of SQL Server do you have?
2. Does your string always confirm to XML rules (i.e. XHTML)?
Reply
  #4  
Old January 23rd, 2007, 04:35 PM
iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
Default

[PHP]Declare @sHTML varchar(50), @Tag1 varchar(50), @Tag2 varchar(50)
Set @Tag1 = '<font size=3>'
Set @Tag2 = '</font>'

select Replace(Replace( '<font size=3>I love my job</font>',@Tag1,''),@Tag2, '')[/PHP]
Reply
  #5  
Old January 24th, 2007, 06:48 AM
Newbie
 
Join Date: Jan 2007
Posts: 2
Default

Quote:
Originally Posted by almaz
1. What version of SQL Server do you have?
2. Does your string always confirm to XML rules (i.e. XHTML)?

Hi

I use a SQL Server 2005.
Reply
  #6  
Old January 24th, 2007, 07:58 AM
almaz's Avatar
Expert
 
Join Date: Dec 2006
Location: Kyiv, Ukraine
Age: 28
Posts: 167
Default

I assume that your task is to remove all tags from the string.
SQL Server is not well-suited for string manipulations, so in SQL Server 2005 I would recommend using CLR function that will do the trick. If you don't want to invoke .NET for this task, this sample may solve your problem:
Expand|Select|Wrap|Line Numbers
  1. declare @sHTML nvarchar(100)
  2. set @sHTML = '<html><body>Hello World, <font size="3">I love my job</font></body></html>'
  3. declare @i int
  4.  
  5. set @i = patindex('%<%>%', @sHTML)
  6. while @i > 0
  7. begin
  8.     set @sHTML = stuff(@sHTML, @i, charindex('>', @sHTML, @i) - @i + 1, '')
  9.     set @i = patindex('%<%>%', @sHTML)
  10. end
  11. print @sHTML
  12.  
Reply
  #7  
Old February 21st, 2007, 01:56 PM
Newbie
 
Join Date: Feb 2007
Posts: 1
Default

Quote:
Originally Posted by almaz
I assume that your task is to remove all tags from the string.
SQL Server is not well-suited for string manipulations, so in SQL Server 2005 I would recommend using CLR function that will do the trick. If you don't want to invoke .NET for this task, this sample may solve your problem:
Expand|Select|Wrap|Line Numbers
  1. declare @sHTML nvarchar(100)
  2. set @sHTML = '<html><body>Hello World, <font size="3">I love my job</font></body></html>'
  3. declare @i int
  4.  
  5. set @i = patindex('%<%>%', @sHTML)
  6. while @i > 0
  7. begin
  8.     set @sHTML = stuff(@sHTML, @i, charindex('>', @sHTML, @i) - @i + 1, '')
  9.     set @i = patindex('%<%>%', @sHTML)
  10. end
  11. print @sHTML
  12.  
I have another problem, for example if i have situation like:

set @sHTML = '<P><FONT style=%22BACKGROUND-COLOR: #ffff99%22><STRONG><FONT color=#009900></FONT></STRONG></FONT></P><P>bbbb</P>'

this query fails because of % in html tag and it doesn't replace it well...
how can i solve this?
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.