Cast(x AS STRING) casts to integer in SQLite
lalitm.com·16h·
Flag this post

Nov 3, 2025

As an “SQLite consultant” for my local area of Google, I often have people come to me having written SQL like:

SELECT CAST(bar AS STRING) AS baz
FROM foo

and ask me “Why is baz always an integer?! Have I hit an SQLite bug?”.

I have to again reach for my list of “odd quirks that SQLite has that people don’t know about”. Because this is not a bug, at least according to the SQLite manual.

Instead, the correct way to write the above query is:

SELECT CAST(bar AS TEXT) AS baz
FROM foo

The reason for this? Quoting from “Determination of Column Affinity”

For tables not declared as STRICT, the affinity of a column is determined by the declared type of the column, according to the following rules in the …

Similar Posts

Loading similar posts...