Search code examples
mysqlsql-liketrailing-whitespace

Mysql LIKE script ignores trailing space pattern


I have a value 8122 which has a trailing space at the end. Why does my sql script isn't catching this? This is the sql script:

select * from values where value like '% ' or value like ' %';

How to set the LIKE to be more sensitive in this sense?


Solution

  • Check the length of the tsrngsm before and after trimming

    values is a reserved word in mysql

    SELECT 
        * 
    FROM `values `
    WHERE LENGTH(value) > LENGTH(TRIM(value));
    

    If you only want to oheck right spaces you use RTRIM and for left LTRIM

    Incase you are looking for only one space you must check

    SELECT 
        * 
    FROM `values `
    WHERE LENGTH(value)-1 = LENGTH(RTRIM(value))
        OR LENGTH(value)-1 = LENGTH(LTRIM(value));