Create a gist now

Instantly share code, notes, and snippets.

Scalar function to reduce all repeating space characters down to a single space character
CREATE FUNCTION [dbo].[deDupeSpaces]
(
@input nvarchar(500)
)
RETURNS nvarchar(500)
AS
BEGIN
/**
* Based on Nigel Rivett's SQL script found:
* http://www.nigelrivett.net/SQLTsql/RemoveNonNumericCharacters.html
*/
DECLARE @i int
set @i = patindex('%[ ][ ]%', @input)
while @i > 0
begin
set @input = replace(@input, ' ', ' ')
set @i = patindex('%[ ][ ]%', @input)
end
RETURN @input
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment