SQL method to replace repeating blanks with single blanks

前端 未结 15 2025
既然无缘
既然无缘 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
    

提交回复
热议问题