← All stories
● Covered by 1 source Β· 1 reportMedium impact

SQLite Supports NUL Characters in Strings but Causes Confusion

Aggregated by BrevFeed dev Β· updated 1h ago
πŸ”– Save

SQLite permits NUL characters in string values, which can lead to unexpected behaviors in SQL functions like length() and quote(). These quirks can confuse users, as demonstrated by specific SQL queries that yield inconsistent results due to the presence of NUL characters.

Key points

Introduction of NUL Characters in SQLite

SQLite permits the storage of NUL characters (ASCII 0x00) in string values, which introduces unexpected behaviors. Users may not be aware of how these characters affect SQL functions and commands.

Behavior of SQL Functions

The length() SQL function only counts characters up to the first NUL character. For example, if a string contains 'abc'||char(0)||'xyz', the length() function would return 3, leading to potential confusion for users. Similar behavior is seen with the quote() function and the .dump command in the CLI, which omit any content following the first NUL character.

Implications for Querying

When querying strings containing NUL characters, the results may not match expectations. In the provided SQL example, querying for a string with an embedded NUL character returns zero rows, despite what the length() function suggests. This behavior may be interpreted as a bug by users unfamiliar with how SQLite handles NUL characters.

Viewing and Removing NUL Characters

To visualize the full length of strings with embedded NULs, users can cast strings to BLOBs, allowing them to see every byte. Additionally, a SQL expression like instr(X,char(0)) can identify the presence of NUL characters within strings. To clean up such strings, SQL UPDATE statements can be executed to remove NUL characters from columns.

✨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors β€” check the original sources. How BrevFeed works β†’

Reporting from

SQLite permits NUL characters in string values, which can lead to unexpected behaviors in SQL functions like length() and quote(). These quirks can confuse users, as demonstrated by specific SQL queries that yield inconsistent results due to the presence of NUL characters.