SQL method to replace repeating blanks with single blanks

前端 未结 15 1928
既然无缘
既然无缘 2020-12-01 17:52

Is there a more elegant way of doing this. I want to replace repeating blanks with single blanks....

   declare @i int

    set @i=0
    while @i <= 20
           


        
相关标签:
15条回答
  • 2020-12-01 18:26

    Not very SET Based but a simple WHILE would do the trick.

    CREATE TABLE #myTable (myTextColumn VARCHAR(32))
    
    INSERT INTO #myTable VALUES ('NoSpace')
    INSERT INTO #myTable VALUES ('One Space')
    INSERT INTO #myTable VALUES ('Two  Spaces')
    INSERT INTO #myTable VALUES ('Multiple    Spaces    .')
    
    WHILE EXISTS (SELECT * FROM #myTable WHERE myTextColumn LIKE '%  %')
      UPDATE  #myTable 
      SET     myTextColumn = REPLACE(myTextColumn, '  ', ' ') 
      WHERE   myTextColumn LIKE '%  %'
    
    SELECT * FROM #myTable
    
    DROP TABLE #myTable
    
    0 讨论(0)
  • 2020-12-01 18:35

    This works:

    UPDATE myTable
    SET myTextColumn =
        REPLACE(
            REPLACE(
                REPLACE(myTextColumn
                    ,'  ',' '+CHAR(1)) -- CHAR(1) is unlikely to appear
            ,CHAR(1)+' ','')
        ,CHAR(1),'')
    WHERE myTextColumn LIKE '%  %'
    

    Entirely set-based; no loops.

    So we replace any two spaces with an unusual character and a space. If we call the unusual character X, 5 spaces become: ' X X ' and 6 spaces become ' X X X'. Then we replace 'X ' with the empty string. So 5 spaces become ' ' and 6 spaces become ' X'. Then, in case there was an even number of spaces, we remove any remaining 'X's, leaving a single space.

    0 讨论(0)
  • 2020-12-01 18:35

    Try it:

    CREATE OR REPLACE FUNCTION REM_SPACES (TEXTO VARCHAR(2000))
    
        RETURNS VARCHAR(2000)
    
        LANGUAGE SQL
    
        READS SQL DATA
    BEGIN
    
        SET TEXTO = UPPER(LTRIM(RTRIM(TEXTO)));
    
        WHILE LOCATE('  ',TEXTO,1) >= 1 DO
            SET TEXTO = REPLACE(TEXTO,'  ',' ');
        END WHILE;
    
    RETURN TEXTO;
    
    END
    
    0 讨论(0)
  • 2020-12-01 18:36
    create table blank(
    field_blank char(100))
    
    insert into blank values('yyy      yyyy')
    insert into blank values('xxxx    xxxx')
    insert into blank values ('xxx    xxx')
    insert into blank values ('zzzzzz zzzzz')
    
    update blank 
    set field_blank = substring(field_blank,1,charindex(' ',field_blank)-1) + ' ' + ltrim(substring(field_blank,charindex(' ',field_blank) + 1,len(field_blank)))
    where CHARINDEX (' ' , rtrim(field_blank)) > 1
    
    select * from blank
    
    0 讨论(0)
  • 2020-12-01 18:37

    Lets say, your Data like this

    Table name : userdata Field: id, comment, status, 
    
     id,  "I love -- -- - -spaces  -- -    my INDIA" , "Active" <br>
     id,  "I love -- -- - -spaces  -- -    my INDIA" , "Active" <br>
     id,  "I love -- -- - -spaces  -- -    my INDIA" , "Active" <br>
     id,  "I love -- -- - -spaces  -- -    my INDIA" , "Active" <br>
    

    So just do like this

    update userdata set comment=REPLACE(REPLACE(comment," ","-SPACEHERE-"),"-SPACEHERE"," ");
    

    I didn't tested , but i think this will work.

    0 讨论(0)
  • 2020-12-01 18:41

    Here is a simple set based way that will collapse multiple spaces into a single space by applying three replaces.

    DECLARE @myTable TABLE (myTextColumn VARCHAR(50))
    
    INSERT INTO @myTable VALUES ('0Space')
    INSERT INTO @myTable VALUES (' 1 Spaces 1 Spaces. ')
    INSERT INTO @myTable VALUES ('  2  Spaces  2  Spaces.  ')
    INSERT INTO @myTable VALUES ('   3   Spaces  3   Spaces.   ')
    INSERT INTO @myTable VALUES ('    4    Spaces  4    Spaces.    ')
    INSERT INTO @myTable VALUES ('     5     Spaces  5     Spaces.     ')
    INSERT INTO @myTable VALUES ('      6      Spaces  6      Spaces.      ')
    
    select replace(
              replace(
                 replace(
                    LTrim(RTrim(myTextColumn)), ---Trim the field
                 '  ',' |'),                    ---Mark double spaces
              '| ',''),                         ---Delete double spaces offset by 1
           '|','')                              ---Tidy up
           AS SingleSpaceTextColumn
     from @myTable
    

    Your Update statement can now be set based:

     update @myTable
        set myTextColumn = replace(
                              replace(
                                 replace(
                                    LTrim(RTrim(myTextColumn)),
                                 '  ',' |'),
                              '| ',''),
                           '|','')  
    

    Use an appropriate Where clause to limit the Update to only the rows that have you need to update or maybe have double spaces.

    Example:

    where 1<=Patindex('%  %', myTextColumn)
    

    I have found an external write up on this method: REPLACE Multiple Spaces with One

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