Search code examples
mysqlreplacewhitespacetrimremoving-whitespace

removing whitespaces not working MySQL. Used TRIM() and REPLACE()


Take a look : FIDDLE

select IF((TRIM(replace(' IKECHUKWU  OSUJI',' ',''))=TRIM(replace('IKECHUKWU OSUJI',' 
',''))),"same","diff");
select IF((TRIM(replace(' Aman Minhas  ',' ',''))=TRIM(replace(' Aman Min has',' 
',''))),"same","diff");

Err image The first query returns diff. The second returns same. Its some weird spacing issue, cant seem to understand why this behaviour.


Solution

  • Your first string has a tab in it:

    select IF((TRIM(replace(' IKECHUKWU  OSUJI',' ',''))
                                       ^ this is actually a tab in the Fiddle
    

    You can get rid of it with an additional REPLACE:

    REPLACE(REPLACE(myString, ' ', ''), '\t', '')
    

    The \t is a special literal. Other special literals such as newline or ASCII NUL may impact you as well. Literals are listed here.