Showing posts with label Function to remove Multiple Spaces. Show all posts
Showing posts with label Function to remove Multiple Spaces. Show all posts

Tuesday, February 16, 2010

SQL Server 2005:Function To Remove Multiple spaces

Function to remove Multiple Spaces
"
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[removeMultiSpaces]
(
@InString VARCHAR(1024)
)
RETURNS VARCHAR(1024)
AS
BEGIN
WHILE CHARINDEX(' ',@ InString) > 0 -- Checking for double spaces
SET @InString = REPLACE(@InString,' ',' ') -- Replace 2 spaces with 1 space
RETURN @InString

END "

Example:
select [dbo].[fnRemoveMultipleSpaces]('Hi Testing')
Result
Hi Testing