Remove all spaces from a string in SQL Server

后端 未结 23 770
死守一世寂寞
死守一世寂寞 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 01:56

    I would use a REPLACE

    select REPLACE (' Hello , How Are You ?', ' ', '' )
    

    REPLACE

    0 讨论(0)
  • 2020-11-28 01:56

    Just a tip, in case you are having trouble with the replace function, you might have the datatype set to nchar (in which case it is a fixed length and it will not work).

    0 讨论(0)
  • 2020-11-28 01:57

    To make all of the answers above complete, there are additional posts on StackOverflow on how to deal with ALL whitespace characters (see https://en.wikipedia.org/wiki/Whitespace_character for a full list of these characters):

    • TSQL 2008 Using LTrim(RTrim and still have spaces in the data
    • How do I remove non-breaking spaces from a column in SQL server?
    • What's a good way to trim all whitespace characters from a string in T-SQL without UDF and without CLR?
    0 讨论(0)
  • 2020-11-28 01:58

    Simply replace it;

    SELECT REPLACE(fld_or_variable, ' ', '')
    

    Edit: Just to clarify; its a global replace, there is no need to trim() or worry about multiple spaces for either char or varchar:

    create table #t (
        c char(8),
        v varchar(8))
    
    insert #t (c, v) values 
        ('a a'    , 'a a'    ),
        ('a a  '  , 'a a  '  ),
        ('  a a'  , '  a a'  ),
        ('  a a  ', '  a a  ')
    
    select
        '"' + c + '"' [IN], '"' + replace(c, ' ', '') + '"' [OUT]
    from #t  
    union all select
        '"' + v + '"', '"' + replace(v, ' ', '') + '"'
    from #t 
    

    Result

    IN             OUT
    ===================
    "a a     "     "aa"
    "a a     "     "aa"
    "  a a   "     "aa"
    "  a a   "     "aa"
    "a a"          "aa"
    "a a  "        "aa"
    "  a a"        "aa"
    "  a a  "      "aa"
    
    0 讨论(0)
  • 2020-11-28 01:58

    Check and Try the below script (Unit Tested)-

    --Declaring
    DECLARE @Tbl TABLE(col_1 VARCHAR(100));
    
    --Test Samples
    INSERT INTO @Tbl (col_1)
    VALUES
    ('  EY     y            
    Salem')
    , ('  EY     P    ort       Chennai   ')
    , ('  EY     Old           Park   ')
    , ('  EY   ')
    , ('  EY   ')
    ,(''),(null),('d                           
        f');
    
    SELECT col_1 AS INPUT,
        LTRIM(RTRIM(
        REPLACE(
        REPLACE(
        REPLACE(
        REPLACE(
        REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(col_1,CHAR(10),' ')
            ,CHAR(11),' ')
            ,CHAR(12),' ')
            ,CHAR(13),' ')
            ,CHAR(14),' ')
            ,CHAR(160),' ')
            ,CHAR(13)+CHAR(10),' ')
        ,CHAR(9),' ')
        ,' ',CHAR(17)+CHAR(18))
        ,CHAR(18)+CHAR(17),'')
        ,CHAR(17)+CHAR(18),' ')
        )) AS [OUTPUT]
    FROM @Tbl;
    
    0 讨论(0)
  • 2020-11-28 02:04

    this is useful for me:

    CREATE FUNCTION dbo.TRIM(@String VARCHAR(MAX))
    RETURNS VARCHAR(MAX)
    BEGIN
        RETURN LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String,CHAR(10),'[]'),CHAR(13),'[]'),char(9),'[]'),CHAR(32),'[]'),'][',''),'[]',CHAR(32))));
    END
    GO
    

    .

    0 讨论(0)
提交回复
热议问题