Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicted Regex Replace

Author  Topic 

SergioM
Posting Yak Master

170 Posts

Posted - 2013-11-22 : 14:18:39
I'm not even sure if this can be done in TSQL or if I'll have to write something separately. I would like to replace image URLs from description fields which are within a cell based on another table. The file name remains constant, but the location changes.

Example: DescriptionTable
id	description
1 This item is for sale with a 20% coupon <img src="http://mysite.com/321654987321654987.jpg">
2 Everyone needs a Widget! Buy one today! <img src="http://yoursite.com/test/159481592658165165.jpg">
3 This product will make you happy <img src="http://tmpsite.org/984651513849846516.jpg">


Example: ReplacementTable
id	original	replacement
1 http://mysite.com/ http://NEWPlace.com/
2 http://yoursite.com/test/ https://localPage.com/test/
3 http://tmpsite.org/ http://temporary.org/


Here we see that DescriptionTable has the information written within an HTML block. ImageReplacement has unique IDs that do not appear casually within the code. They can only refer to the link I want to replace.

If I had to write software, I would SELECT each description cell & do a RegexFind per url, then SELECT the original + replacement & finally do a RegexReplace. Is there a way to do that in TSQL?

-Sergio
I use Microsoft SQL 2008

robvolk
Most Valuable Yak

15732 Posts

Posted - 2013-11-22 : 14:43:38
There's no built-in regex support in SQL Server, however what you describe should work as follows:
UPDATE D SET Description=REPLACE(Description,R.Original,R.Replacement)
FROM DescriptionTable D
INNER JOIN ReplacementTable R ON D.Description LIKE '%' + R.Original + '%'
Go to Top of Page
   

- Advertisement -