MySQL : how to remove double or more spaces from a string?

前端 未结 11 1200
南方客
南方客 2020-12-03 08:12

I couldn\'t find this question for MySQL so here it is:

I need to trim all double or more spaces in a string to 1 single space.

For example: \"The  

相关标签:
11条回答
  • 2020-12-03 08:31

    If you are using php....

    try{
    $con = new PDO ("mysql:host=localhost;dbname=dbasename","root","");
    }
    catch(PDOException $e){
    echo "error".$e-getMessage();   
    }
    
    $select = $con->prepare("SELECT * FROM table");
    $select->setFetchMode(PDO::FETCH_ASSOC);
    $select->execute();
    
    while($data=$select->fetch()){ 
    
    $id = $data['id'];
    $column = $data['column'];
    
    $column = trim(preg_replace('/\s+/',' ', $column)); // remove all extra space
    
    
    $update = $con->prepare("UPDATE table SET column=:column WHERE id='$id'");
    $update->bindParam(':column', $column );
    $update->execute();
    
    // echo $column."<br>";
    } 
    
    0 讨论(0)
  • 2020-12-03 08:34

    This solution isn't very elegant but since you don't have any other option:

    UPDATE t1 set str = REPLACE( REPLACE( REPLACE( str, "  ", " " ), "  ", " " ), "  ", " " );
    
    0 讨论(0)
  • 2020-12-03 08:37

    I know this question is tagged with mysql, but if you're fortunate enough to use MariaDB you can do this more easily:

    SELECT REGEXP_REPLACE(column, '[[:space:]]+', ' ');
    
    0 讨论(0)
  • 2020-12-03 08:37

    After searching I end up writing a function i.e

    drop function if exists trim_spaces;

    delimiter $$
    
    CREATE DEFINER=`root`@`localhost` FUNCTION `trim_spaces`(`dirty_string` text, `trimChar` varchar(1))
        RETURNS text
        LANGUAGE SQL
        NOT DETERMINISTIC
        CONTAINS SQL
        SQL SECURITY DEFINER
        COMMENT ''
    BEGIN
      declare cnt,len int(11) ;
      declare clean_string text;
      declare chr,lst varchar(1);
    
      set len=length(dirty_string);
      set cnt=1;  
      set clean_string='';
    
     while cnt <= len do
          set  chr=right(left(dirty_string,cnt),1);           
    
          if  chr <> trimChar OR (chr=trimChar AND lst <> trimChar ) then  
              set  clean_string =concat(clean_string,chr);
          set  lst=chr;     
         end if;
    
         set cnt=cnt+1;  
      end while;
    
      return clean_string;
    END
    $$
    delimiter ;
    

    USAGE:

    set @str='------apple--------banana-------------orange---' ;

    select trim_spaces( @str,'-')

    output: apple-banana-orange-

    parameter trimChar to function could by any character that is repeating and you want to remove .

    Note it will keep first character in repeating set

    cheers :)

    0 讨论(0)
  • 2020-12-03 08:39

    If you want to update the existing column which has multiple spaces into one then this update query will be helpful:

    UPDATE your_table SET column_that_you_want_to_change= REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');

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