Это дает именно то, что вам нужно. Прежде всего создайте функцию. Я использую ваши данные для примера.
CREATE FUNCTION [dbo].[RegexReplace]
(
@pattern VARCHAR(255),
@replacement VARCHAR(255),
@Subject VARCHAR(4000),
@global BIT = 1,
@Multiline bit =1
)
RETURNS VARCHAR(4000)
/*MORE DETAILS @ http://www.simple-talk.com/sql/t-sql-programming/tsql-regular-expression- workbench/ */
AS BEGIN
DECLARE @objRegexExp INT,
@objErrorObject INT,
@strErrorMessage VARCHAR(255),
@Substituted VARCHAR(8000),
@hr INT,
@Replace BIT
SELECT @strErrorMessage = 'creating a regex object'
EXEC @hr= sp_OACreate 'VBScript.RegExp', @objRegexExp OUT
IF @hr = 0
SELECT @strErrorMessage = 'Setting the Regex pattern',
@objErrorObject = @objRegexExp
IF @hr = 0
EXEC @hr= sp_OASetProperty @objRegexExp, 'Pattern', @pattern
IF @hr = 0 /*By default, the regular expression is case sensitive. Set the IgnoreCase property to True to make it case insensitive.*/
SELECT @strErrorMessage = 'Specifying the type of match'
IF @hr = 0
EXEC @hr= sp_OASetProperty @objRegexExp, 'IgnoreCase', 0
IF @hr = 0
EXEC @hr= sp_OASetProperty @objRegexExp, 'MultiLine', @Multiline
IF @hr = 0
EXEC @hr= sp_OASetProperty @objRegexExp, 'Global', @global
IF @hr = 0
SELECT @strErrorMessage = 'Doing a Replacement'
IF @hr = 0
EXEC @hr= sp_OAMethod @objRegexExp, 'Replace', @Substituted OUT,
@subject, @Replacement
/*If the RegExp.Global property is False (the default), Replace will return the @subject string with the first regex match (if any) substituted with the replacement text. If RegExp.Global is true, the @Subject string will be returned with all matches replaced.*/
IF @hr <> 0
BEGIN
DECLARE @Source VARCHAR(255),
@Description VARCHAR(255),
@Helpfile VARCHAR(255),
@HelpID INT
EXECUTE sp_OAGetErrorInfo @objErrorObject, @source OUTPUT,
@Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT
SELECT @strErrorMessage = 'Error whilst '
+ COALESCE(@strErrorMessage, 'doing something') + ', '
+ COALESCE(@Description, '')
RETURN @strErrorMessage
END
EXEC sp_OADestroy @objRegexExp
RETURN @Substituted
END
--EXAMPLE
DECLARE @YourLink AS VARCHAR(1000)
SELECT @YourLink = '<a href="_UpInclude/scriptEx/__TQinfoBaseImage.asp? rq_RecordId=28177&rq_MasterId=28177&rq_Revision=1">Link1</a>'
SELECT '<a href="/ror/current/' +
dbo.RegexReplace('.*__TQinfoBaseImage.asp?.*rq_MasterId=(\d+).*', '$1',@YourLink,1,1)
+ '.image">'
+ dbo.RegexReplace('<a\b[^>]*>(.*?)</ ?a>', '$1',@YourLink,1,1)
+ '</a>'
/ * Замените @YourLink именем столбца данных, чтобы получить результаты и обновить * /