SQLite Tutorial SQLite Advanced SQLite Database SQLite References

SQLite GLOB() Function



The SQLite GLOB() function is used in a WHERE clause to search for a specified pattern in a specified column.

  • The GLOB(X,Y) function is used to implement the "Y GLOB X" expression.
  • The NOT keyword can be used with this function to implement "NOT GLOB" expression.
Note: Unlike LIKE() function, GLOB() is case sensitive and it follows syntax of UNIX for specifying the given wildcards.

The wildcards which are used in conjunction with this function are given below:

Wildcard Characters in SQLite

SymbolDescriptionExample
*Represents zero, one or multiple characters.'J*' represents a value that start with "J", for example - John, Jo and Jack etc.
?Represents one character.'?o*' represents a value that have "o" in the second position, for example - John, Jo and Journey etc.

Syntax

The syntax for using GLOB() function in SQLite is given below:

/* Using GLOB() function */
SELECT column1, column2, ...
FROM table_name
WHERE GLOB(pattern, column);

/* Using NOT GLOB() function */
SELECT column1, column2, ...
FROM table_name
WHERE NOT GLOB(pattern, column);

The table below describes patterns which is used with GLOB() function and uses (%) and (_).

PatternDescription
'J*'A value that start with "J".
'*n'A value that end with "n".
'*oh*'A value that have "oh" in any position.
'?o*'A value that have "o" in the second position.
'J?*'A value that start with "J" and have at least 2 characters.
'J??*'A value that start with "J" and have at least 3 characters.
'J*n'A value that start with "J" and ends with "n".

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • Using the * Wildcard : To select all records of the Employee table with Name starting with 'Jo', the query is given below.

    SELECT * FROM Employee
    WHERE GLOB('Jo*', Name);
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    3JoParis272800
  • Using the * Wildcard with NOT GLOB() function: NOT GLOB() function is used as the negation of GLOB() function. For example, to select all records of the Employee table with Name not starting with 'Jo', the following query can be used:

    SELECT * FROM Employee
    WHERE NOT GLOB('Jo*', Name);
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    2MarryNew York242750
    4KimAmsterdam303100
    5RameshNew Delhi283000
    6HuangBeijing282800
  • Using the ? Wildcard : To select all records of the Employee table with Name containing 'o' as second character, the query is mentioned below.

    SELECT * FROM Employee
    WHERE GLOB('?o*', Name);
    

    The result of the above code will be:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    3JoParis272800
  • Using the ? Wildcard with NOT GLOB() function: To select all records of the Employee table with Name not containing 'o' as second character, the query is given below:.

    SELECT * FROM Employee
    WHERE NOT GLOB('?o*', Name);
    

    The result of the above code will be:

    EmpIDNameCityAgeSalary
    2MarryNew York242750
    4KimAmsterdam303100
    5RameshNew Delhi283000
    6HuangBeijing282800

❮ SQLite Functions