Home > OS >  Delete blank spaces in a string column SQL Server
Delete blank spaces in a string column SQL Server

Time:11-04

I have sql table with the following values: 'test1 ', 'test2 '. I need to delete all blank spaces in a string.

It looks easy but TRIM, LTRIM, RTRIM or REPLACE(column,' ','') does not work.

LEN() function count that space as a character. Lenght of value 'test1 ' is 6.

In which way can I select that column without that blank space? I need value 'test1'.

CodePudding user response:

The minimal reproducible example is not provided.

Please try the following solution.

SQL

USE tempdb;
GO

DROP FUNCTION IF EXISTS dbo.udf_tokenize;
GO

/*
1. All invisible TAB, Carriage Return, and Line Feed characters will be replaced with spaces.
2. Then leading and trailing spaces are removed from the value. 
3. Further, contiguous occurrences of more than one space will be replaced with a single space.
*/
CREATE FUNCTION dbo.udf_tokenize(@input VARCHAR(MAX))
   RETURNS VARCHAR(MAX)
AS
BEGIN 
   RETURN (SELECT CAST('<r><![CDATA['   @input   ' '   ']]></r>' AS XML).value('(/r/text())[1] cast as xs:token?','VARCHAR(MAX)'));
END
GO

-- DDL and sample data population, start
DECLARE @mockTbl TABLE (ID INT IDENTITY(1,1), col_1 VARCHAR(100), col_2 VARCHAR(100));
INSERT INTO @mockTbl (col_1, col_2)
VALUES ('  FL   ', '  Miami')
   , ('  FL   ', '  Fort       Lauderdale   ')
   , ('  NY   ', '  New           York   ')
   , ('  NY   ', '')
   , ('  NY   ', NULL);
-- DDL and sample data population, end

-- before
SELECT * FROM @mockTbl;

-- remove invisible chars
UPDATE @mockTbl
SET col_1 = dbo.udf_tokenize(col_1)
, col_2 = dbo.udf_tokenize(col_2);

-- after
SELECT *, LEN(col_2) AS [col_2_len] FROM @mockTbl; 

CodePudding user response:

As discovered in the comments, the character(s) at the end of your value isn't a whitespace, it's a carriage return. LTRIM and RTRIM don't remove these characters, and TRIM only removes whitespace (character 32) by default.

If you want to remove some other characters, you can use TRIM, but you need to tell it to remove said other characters, using the TRIM({Characters} FROM {String}) syntax. The below removes leading and trailing Spaces (' '), Carriage Returns (CHAR(13)) and Line Breaks (CHAR(10)):

CREATE TABLE dbo.YourTable (SomeString varchar(50));
GO

INSERT INTO dbo.YourTable (SomeString)
VALUES('Trailing Space'),
      ('Trailing Line Break'   CHAR(10)),
      ('Trailing CRLF'   CHAR(13)   CHAR(10)),
      ('Trailing CRLF and spaces '   CHAR(13)   CHAR(10)   '  ');
GO

DECLARE @TrimCharacters varchar(10) = ' '   CHAR(13)   CHAR(10);

SELECT SomeString,
       LEN(SomeString) AS Len,
       DATALENGTH(SomeString) AS DataLength,
       TRIM(SomeString) AS Trimmed,
       DATALENGTH(TRIM(SomeString)) AS TrimmedDataLength,
       TRIM(@TrimCharacters FROm SomeString) AS WellTrimmed,
       DATALENGTH(TRIM(@TrimCharacters FROm SomeString)) AS WellTrimmedDataLength
FROM dbo.YourTable;
GO

DROP TABLE dbo.YourTable;

CodePudding user response:

You can use the function below to check if each character of the string is within the ASCII values 32 to 126 (Numbers & Alphabet). This will remove the "character" that's not within the ASCII range.

SQL:

DECLARE @TestString varchar(10) = 'test1 '
DECLARE @Result nvarchar(max)
SET @Result = ''
 
DECLARE @character nvarchar(1)
DECLARE @characterposition int
 
SET @characterposition = 1
WHILE @characterposition <= LEN(@TestString)
BEGIN
    SET @character = SUBSTRING(@TestString , @characterposition, 1)
 
    IF ASCII(@character) >= 32 AND ASCII(@character) <= 126
        SET @Result = @Result   @character
    SET @characterposition = @characterposition   1
END
 
SELECT @Result
  • Related