Replace Double Space With Single Space

Posted: November 22, 2011 in SQL Server 2005

To find the nth maximum record, you can try the following:

-- Declare variables
DECLARE @lv_string VARCHAR(500);

-- Initialise variable
SET @lv_string = 'Do something      with      the    result  !';

-- Loop until all double spaces are replaced with single space
WHILE CHARINDEX(SPACE(2), @lv_string) > 0 
BEGIN
   SET @lv_string = REPLACE(@lv_string, SPACE(2), SPACE(1));
END;

-- Return string
SELECT @lv_string;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s