Skip to content

Instantly share code, notes, and snippets.

@gowon
Created May 24, 2016 19:52
Show Gist options
  • Save gowon/e9aa9e8e0c9da85cdd2cdea81a796154 to your computer and use it in GitHub Desktop.
Save gowon/e9aa9e8e0c9da85cdd2cdea81a796154 to your computer and use it in GitHub Desktop.
TSQL Regex Replace Scalar Function
CREATE FUNCTION [dbo].[RegexReplace](@input VARCHAR(MAX), @pattern VARCHAR(MAX), @replacement VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
WHILE PATINDEX(@pattern, @input) > 0
SET @input = STUFF(@input, PATINDEX(@pattern, @input), 1, @replacement)
RETURN @input
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment