Search code examples
sqlitetruthiness

Get truth value of column value in sqlite query


If I have a column in a table of zero and nonzero numbers, how do I get the {0,1} truth value of it?

Data

x, y
0, 111
1, 0
2, 444

(sql query, something like select x, TRUTHOF(y) from Data)

Result

x, y_truth
0, 1
1, 0
2, 1

Using sqlite3 in python.


Solution

  • In SQLite, boolean expressions return 0 or 1, so you can just compare against zero:

    SELECT x, y != 0 AS y_truth FROM MyTable