skip to Main Content

How to find all rows whose column value has either a leading or trailing space (either single or multiple or just empty. I would like to filter all rows which has the above condition.

This is what I tried

select '"' || url || '"' from urls;

2

Answers


  1. Chosen as BEST ANSWER

    SELECT * FROM table WHERE column_name <> REGEXP_REPLACE(column_name, '^s+|s+$', '');

    The above seems to work


  2. Postgres provides a full set on String Functions. Here use length() and trim(). Trim() and companion functions ltrim(), and rtrim(), removes any leading and/or trailing spaces. The length()` function returns the number of characters/bytes in the string. So here select those rows where the length of the string is not the same as the length of the string with leading and trailing spaced removed.

    select * 
      from <table_name> 
     where length(<column_name>) <> length(trim(<column_name>));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search