akki akki - 1 month ago 9
SQL Question

Best way to add target ='_blank' for href in a string using UDF

I've got data in sql server 2012 that contains text with links

<a href="test/test">test </a>
with out target i need to add target for the string data. I need to create a User defined function for that. Does any one have any sample code for this.

Example text -

If you've ever thought one of your text message threads was so good it deserved to be published, you may be on to something.view. <a href="/apple/sdffsd/sdffs">njkhj </a> In future iterations, users will be able to post .<br><br>You may update your email address at any time by going to <a href="/apple/fafasdf/sdfsdf">sdffsd</a>. Our whole goal is to have the reader go through an entire narrative arc in five minutes and consume it in a way that's native to mobile," says Gupta go to <a href="/a/s/sdfsf"> sdfsdf </a> which is one of my favorite books, is told as letters back and forth between the two main

Answer

I think a simple replace would do the trick

Declare @String varchar(max) = 'Some large text with a <a href="test/test">link</a> to content'

Select Replace(@String,' href=','_target="blank" href=')

Returns

(No column name)
Some large text with a <a _target="blank" href="test/test">link</a> to content
Comments