Home > Blockchain >  How to replace regular expression?
How to replace regular expression?

Time:10-15

I do replacing

SET @data = REPLACE(@data, 'Riched20 10.0.19041}', '');

It is ok, but recently I have detected that it can be

Riched20 10.0.18362 etc

How can I replace in common case like 'Riched20 ...}'?

Can I use a regular-expression?

It should be implementation in T-SQL

CodePudding user response:

If the problem is as simple as this question makes it out to be then you could, instead, find the position of the string 'Riched20' in your value, and then the position of the first } that appears after it and use STUFF to remove the text in that range.

This assumes that the value will always have a } after 'Riched20', and that if there is a } then 'Riched20' also appears. If this isn't the case you will get the value NULL.

DECLARE @data varchar(100) = 'sdjkafhbgtajl asdgasdf, Riched20 10.0.19041} dlkghbsdfl';

SET @data = STUFF(@data, CHARINDEX('Riched20',@data),CHARINDEX('}',@data,CHARINDEX('Riched20',@data)) - CHARINDEX('Riched20',@data)  1,'');

SELECT @data;

If you need "true" pattern replacement, then you are out of luck; T-SQL does not support this as the comments mention.

  • Related