Search code examples
sqldatabasesqlitesql-limit

How to limit returned rows using max characters in sqlite query


Say I have students table with one column named name as follows:

| name |
--------
Jhon
Natalie Kocher
Jonell Dickson
Irvin
Kiara Audet
Shawna Duvall
Cobey Maryellen
Kenny
Lindsy Taylor

How to get all rows that under 6 characters so that I get the following name:

Jhon
Irvin
Kenny

In case that's not possible at least how to sort / set the order of returned rows start from smallest length of chars to the longest.

Thanks,

About SQLite query


Solution

  • Use the length() function:

    select t.*
    from t
    where length(name) < 6;
    

    Or you can use not like:

    where name not like '______%'  -- there are 6 underscores