Menu

SQLite String Functions: Search, Extract, Replace, and Format Text

SQLite string functions let you find text, extract part of a value, remove unwanted characters, and format output. Use the links below to choose a function for your task.

Task Function What it does
Count characters or BLOB bytes length() Counts Unicode characters in text or bytes in a BLOB
Find a substring instr() Returns the 1-based position of a match, or 0 if absent
Extract a substring substr() Extracts text using a starting position and optional length
Replace matching text replace() Replaces every occurrence of a substring
Remove leading and trailing characters trim() Removes spaces by default or specified characters
Change letter case lower(), upper() Converts ASCII letters to lowercase or uppercase in the default SQLite build
Format text and numbers printf() Formats values using a format string

Example: clean, search, and extract text

Run this query in the SQLite Playground:

SELECT
    trim('  SQLiz  ') AS cleaned,
    instr('SQLite', 'Lite') AS match_position,
    substr('SQLite', 1, 3) AS prefix,
    replace('a-b-a', 'a', 'x') AS replaced,
    length('café') AS characters;

Expected values: cleaned = SQLiz, match_position = 3, prefix = SQL, replaced = x-b-x, and characters = 4.

SQLite string positions are generally 1-based. instr() returns 0 when the search text is not found, whereas substr() extracts from a position you supply. length() counts Unicode characters for text, not UTF-8 bytes. For full case conversion beyond ASCII, SQLite may require an extension such as ICU.

For the complete built-in function definitions and edge cases, see the official SQLite core functions reference. To format a query without executing it, use the SQL Formatter.

  1. char

    The SQLite char() function accepts a series of Unicode code point values ​​as parameters and returns a string consisting of the characters corresponding to these parameters in the order of the parameters.
  2. format

    Use SQLite format() to format strings and numbers in SQLite 3.38.0 and later. It is the renamed printf() SQL function; older versions use printf().
  3. glob

    The SQLite glob() function checks if a given string matches a given pattern.
  4. hex

    The SQLite hex() function treats the parameter as a BLOB and returns a string representing the hexadecimal uppercase form of this BLOB.
  5. instr

    Learn SQLite instr(X,Y) with runnable examples for 1-based matches, missing strings, empty patterns, NULL inputs, Unicode text and BLOB byte offsets.
  6. length

    Learn SQLite length() for Unicode characters, BLOB bytes, NULL and empty strings, with runnable examples and an explanation of embedded NUL characters.
  7. like

    The SQLite like() function checks if a given string matches a given pattern.
  8. lower

    The SQLite lower() function converts the given string to lowercase and returns it.
  9. ltrim

    The SQLite ltrim() function removes the longest string containing all the characters specified by the parameter (whitespace by default) from the start a string.
  10. printf

    Use SQLite printf() to format strings and numbers with %s, %d, %f, and SQLite-specific quoting specifiers. It remains an alias for format().
  11. quote

    The SQLite quote() function takes arguments and return them as SQL literals.
  12. randomblob

    The SQLite randomblob() function returns a blob consisting of a given number of pseudorandom bytes.
  13. replace

    SQLite replace(X,Y,Z) replaces every occurrence of Y in X with Z. Learn how BINARY matching, empty search strings, and replacement-value conversion work.
  14. rtrim

    The SQLite rtrim() function removes the longest string containing all the characters specified by the parameter (whitespace by default) from the end of a string.
  15. substr

    Learn SQLite substr(X,Y,Z) with runnable examples for 1-based positions, negative starts and lengths, NULL inputs, and the substring() alias.
  16. substring

    SQLite substring() is an alias for substr() since SQLite 3.34.0. Learn 1-based positions, optional length, negative indexes and portability with tested examples.
  17. trim

    The SQLite trim() function removes the longest string containing all the characters specified by the parameter (whitespace by default) from the start and end of a string.
  18. unicode

    The SQLite unicode() function returns the Unicode code point of the first character of the specified string.
  19. upper

    The SQLite upper() function converts the given string to uppercase and returns it.
  20. zeroblob

    The SQLite zeroblob() function returns a blob containing the given number of bytes of 0x00.