Skip to content

Instantly share code, notes, and snippets.

@erraggy
Created May 26, 2011 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erraggy/992677 to your computer and use it in GitHub Desktop.
Save erraggy/992677 to your computer and use it in GitHub Desktop.
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