Remove all spaces from a string in SQL Server

后端 未结 23 772
死守一世寂寞
死守一世寂寞 2020-11-28 01:42

What is the best way to remove all spaces from a string in SQL Server 2008?

LTRIM(RTRIM(\' a b \')) would remove all spaces at the right and left of th

相关标签:
23条回答
  • 2020-11-28 02:06

    t-sql replace http://msdn.microsoft.com/en-us/library/ms186862.aspx

    replace(val, ' ', '')

    0 讨论(0)
  • 2020-11-28 02:07

    Try to use like this, if normal spaces are not removed by LTRM or RTRM

    LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(Column_data, CHAR(9), ''), CHAR(10), ''), CHAR(13), '')))
    
    0 讨论(0)
  • 2020-11-28 02:09

    For some reason, the replace works only with one string each time. I had a string like this "Test           MSP" and I want to leave only one space.

    I used the approach that @Farhan did, but with some modifications:

    CREATE FUNCTION ReplaceAll
    (
        @OriginalString varchar(8000),
        @StringToRemove varchar(20),
        @StringToPutInPlace varchar(20)
    )
    RETURNS varchar(8000)
    AS
    BEGIN
    declare @ResultStr varchar(8000)
    set @ResultStr = @OriginalString
    while charindex(@StringToRemove, @ResultStr) > 0
        set @ResultStr = replace(@ResultStr, @StringToRemove, @StringToPutInPlace)
    
    return @ResultStr
    END
    

    Then I run my update like this

    UPDATE tbTest SET Description = dbo.ReplaceAll(Description, '  ', ' ') WHERE ID = 14225
    

    Then I got this result: Test MSP

    Posting here if in case someone needs it as I did.

    Running on: Microsoft SQL Server 2016 (SP2)

    0 讨论(0)
  • 2020-11-28 02:09

    It seems that everybody keeps referring to a single REPLACE function. Or even many calls of a REPLACE function. But when you have dynamic output with an unknown number of spaces, it wont work. Anybody that deals with this issue on a regular basis knows that REPLACE will only remove a single space, NOT ALL, as it should. And LTRIM and RTRIM seem to have the same issue. Leave it to Microsoft. Here's a sample output that uses a WHILE Loop to remove ALL CHAR(32) values (space).

    DECLARE @INPUT_VAL  VARCHAR(8000)
    DECLARE @OUTPUT_VAL VARCHAR(8000)
    
    SET @INPUT_VAL = '      C               A                         '
    SET @OUTPUT_VAL = @INPUT_VAL
    WHILE CHARINDEX(CHAR(32), @OUTPUT_VAL) > 0 BEGIN
        SET @OUTPUT_VAL = REPLACE(@INPUT_VAL, CHAR(32), '')
    END
    
    PRINT 'START:' + @INPUT_VAL + ':END'
    PRINT 'START:' + @OUTPUT_VAL + ':END'
    

    Here's the output of the above code:

    START:      C               A                         :END
    START:CA:END
    

    Now to take it a step further and utilize it in an UPDATE or SELECT statement, change it to a udf.

    CREATE FUNCTION udf_RemoveSpaces (@INPUT_VAL    VARCHAR(8000))
    RETURNS VARCHAR(8000)
    AS 
    BEGIN
    
    DECLARE @OUTPUT_VAL VARCHAR(8000)
    SET @OUTPUT_VAL = @INPUT_VAL
    -- ITTERATE THROUGH STRING TO LOOK FOR THE ASCII VALUE OF SPACE (CHAR(32)) REPLACE IT WITH BLANK, NOT NULL
    WHILE CHARINDEX(CHAR(32), @OUTPUT_VAL) > 0 BEGIN
        SET @OUTPUT_VAL = REPLACE(@INPUT_VAL, CHAR(32), '')
    END
    
    RETURN @OUTPUT_VAL
    END
    

    Then utilize the function in a SELECT or INSERT statement:

    UPDATE A
    SET STATUS_REASON_CODE = WHATEVER.dbo.udf_RemoveSpaces(STATUS_REASON_CODE)
    FROM WHATEVER..ACCT_INFO A
    WHERE A.SOMEVALUE = @SOMEVALUE
    
    INSERT INTO SOMETABLE
    (STATUS_REASON_CODE)
    SELECT WHATEVER.dbo.udf_RemoveSpaces(STATUS_REASON_CODE)
    FROM WHATEVER..ACCT_INFO A
    WHERE A.SOMEVALUE = @SOMEVALUE
    
    0 讨论(0)
  • 2020-11-28 02:10

    REPLACE() function:

    REPLACE(field, ' ', '')
    
    0 讨论(0)
  • 2020-11-28 02:10

    Reference taken from this blog:

    First, Create sample table and data:

    CREATE TABLE tbl_RemoveExtraSpaces
    (
         Rno INT
         ,Name VARCHAR(100)
    )
    GO
    
    INSERT INTO tbl_RemoveExtraSpaces VALUES (1,'I    am     Anvesh   Patel')
    INSERT INTO tbl_RemoveExtraSpaces VALUES (2,'Database   Research and     Development  ')
    INSERT INTO tbl_RemoveExtraSpaces VALUES (3,'Database    Administrator     ')
    INSERT INTO tbl_RemoveExtraSpaces VALUES (4,'Learning    BIGDATA    and       NOSQL ')
    GO
    

    Script to SELECT string without Extra Spaces:

    SELECT
         [Rno]
        ,[Name] AS StringWithSpace
        ,LTRIM(RTRIM(REPLACE(REPLACE(REPLACE([Name],CHAR(32),'()'),')(',''),'()',CHAR(32)))) AS StringWithoutSpace
    FROM tbl_RemoveExtraSpaces
    

    Result:

    Rno         StringWithSpace                                 StringWithoutSpace
    ----------- -----------------------------------------  ---------------------------------------------
    1           I    am     Anvesh   Patel                      I am Anvesh Patel
    2           Database   Research and     Development         Database Research and Development
    3           Database    Administrator                       Database Administrator
    4           Learning    BIGDATA    and       NOSQL          Learning BIGDATA and NOSQL
    
    0 讨论(0)
提交回复
热议问题