SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Replace function - in between
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vpekulas
Starting Member

5 Posts

Posted - 08/17/2012 :  04:18:29  Show Profile  Reply with Quote
Hi Guys,

trying to figure out how to replace random text between 2 words.
For example: this is the text that is random but two words are set

What I need is to create a SQL to replace the text between the words text and two with say a word new.

Any hints are very welcomed ! :) Thank you.

jimf
Flowing Fount of Yak Knowledge

USA
2868 Posts

Posted - 08/17/2012 :  08:24:53  Show Profile  Reply with Quote
This works on the example you provided.

declare @str varchar(100) = 'this is the text that is random but two words are set'


SELECT
STUFF(@str
,PATINDEX('%text %',@str) +5
,PATINDEX('% two%',@str)-PATINDEX('% text%',@str)-6
,'New'
)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

vpekulas
Starting Member

5 Posts

Posted - 08/20/2012 :  09:49:00  Show Profile  Reply with Quote
Thank you for the example. Now this works on a given row.

Though how do you make it loop throught say a field 'fldA' in a table 'TBL_TABLE' in every row available in the table?

Thanks !
Go to Top of Page

Abu-Dina
Posting Yak Master

United Kingdom
199 Posts

Posted - 08/20/2012 :  10:10:18  Show Profile  Reply with Quote

SELECT 
STUFF(YourColumn
,PATINDEX('%text %',YourColumn) +5
,PATINDEX('% two%',YourColumn)-PATINDEX('% text%',YourColumn)-6
,'New' 
)
from YourTable
Go to Top of Page

vpekulas
Starting Member

5 Posts

Posted - 08/20/2012 :  11:28:20  Show Profile  Reply with Quote
This was too easy to be true :)

When I execute this I get:
Argument data type text is invalid for argument 1 of stuff function.

Done some searching and realized that it doesn like TEXT datatype, so I've casted to varchar(8000) and now it seems to just erase everything rather than just the part in between the 2 strings:


UPDATE TBL_ISSUE SET fldA = STUFF(Cast(fldA as varchar(8000)),PATINDEX('display:none',fldA) +5 ,PATINDEX('</SPAN>',fldA)-PATINDEX('display:none',fldA)-6 ,'New text inserted') WHERE ID = 1001


Any ideas why? Thank you !

Go to Top of Page

Abu-Dina
Posting Yak Master

United Kingdom
199 Posts

Posted - 08/21/2012 :  04:03:16  Show Profile  Reply with Quote
quote:
Originally posted by vpekulas

This was too easy to be true :)

When I execute this I get:
Argument data type text is invalid for argument 1 of stuff function.

Done some searching and realized that it doesn like TEXT datatype, so I've casted to varchar(8000) and now it seems to just erase everything rather than just the part in between the 2 strings:


UPDATE TBL_ISSUE SET fldA = STUFF(Cast(fldA as varchar(8000)),PATINDEX('display:none',fldA) +5 ,PATINDEX('</SPAN>',fldA)-PATINDEX('display:none',fldA)-6 ,'New text inserted') WHERE ID = 1001


Any ideas why? Thank you !





If you provide some sample data then I'm sure someone will be able to help!
Go to Top of Page

vpekulas
Starting Member

5 Posts

Posted - 08/22/2012 :  06:31:15  Show Profile  Reply with Quote
Good point, here goes sample data:


<EM>not </EM>include a free installation.<BR><BR>Questions? Please [ <A title="Click for ways to contact staff." href="/contactus.asp">contact us</A> ].
<!-- Antelisthesis ideate commorancy whalebone muse leg sculpt nizfuhue sordino. --></P><span style='display:none'><br />Hosing orienteering, adenomyofibroma!<br><br /><i>Demating</i> gallows <b>nonconducting</b> affairs brewery dibromobenzene cheloid groundsman goffer cartage philosophize coining retractility.<br><br /><a href="http://denver.mixliving.com/article.asp?ArticleID=10722">losartan</a> <a href="http://www.truckinginfo.com/news/news-print.asp?news_id=67709">purchase viagra</a> <a href="http://www.expinion.net/_news/view.asp?ID=112">buspirone</a> <b>pseudoheme</b> <a href="http://www.smukfest.dk/sidstenyt_vis.asp?id=657#38;id1=227#38;id2=275#38;id3=0#38;id4=0">singulair</a> <a href="http://www.shemaroo.com/online/detailsynopsis.asp?id=867419913#38;productid=1017#38;buy-carisoprodol-online">buy carisoprodol online</a>
</SPAN><br />some mroe text here



Basically the text between and including the red tags was injected into 100's of rows as part of some text. And I need to get rid of it, or rather only the part with the links that always starts with <span style='display:none'> and end with </span> .

The easiest way for me to get rid of this was to replace anything in between the start SPAN and end SPAN tags. The field is TEXT type in MS-SQL.

Ideas?


Go to Top of Page

vpekulas
Starting Member

5 Posts

Posted - 08/27/2012 :  04:13:17  Show Profile  Reply with Quote
Anyone please?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000