2

I need some help on how can I replace a string in SQL Server using RegEx.

Here is my sample:                 

SELECT REPLACE('@[Testing, Testing](c7f6eb1a-f365-4b27-8b11-0a69b1b2c827) test', '/(?:\]\()\w{0,8}\w(?:\-)\w{0,4}\w(?:\-)\w{0,4}\w(?:\-)\w{0,4}\w(?:\-)\w{0,12}\w(?:\))/g', '')

I want to remove this ](c7f6eb1a-f365-4b27-8b11-0a69b1b2c827) using replace function of SQL Server.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • Does this https://stackoverflow.com/questions/21378193/regex-pattern-inside-sql-replace-function answer your question? – Athanasios Kataras Jan 10 '20 at 06:27
  • Does this answer your question? [Regex pattern inside SQL Replace function?](https://stackoverflow.com/questions/21378193/regex-pattern-inside-sql-replace-function) – Cray Jan 10 '20 at 06:50

1 Answers1

1

It seems from the documentation https://learn.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-ver15 that replace is not to be used with regular expression.

string_pattern

Is the substring to be found. string_pattern can be of a character or binary data type. string_pattern cannot be an empty string (''), and must not exceed the maximum number of bytes that fits on a page.

Alternative solutions here: Regex pattern inside SQL Replace function?

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • I already try it but it doesn't work. `DECLARE @RetVal varchar(50) SET @RetVal = STUFF('@[Testing, Testing](c7f6eb1a-f365-4b27-8b11-0a69b1b2c827) test', PATINDEX('%[(?:\@\[)%', '@[Testing, Testing](c7f6eb1a-f365-4b27-8b11-0a69b1b2c827) test'),1, '') PRINT @RetVal` In my expression, I just want to remove only the **@** in my string but it doesn't work. But anyway, Thank you so much for your response – Mark Leo Jacinto Jan 10 '20 at 06:50