Selecting rows where remainder (modulo) is 1 after division by 2?

Carlos picture Carlos · Sep 21, 2010 · Viewed 107.2k times · Source

There is a column in options that hold an integer. I want to select the row only if that value % 2 = 1.

I know this can be done in 2 queries but is it possible to do it in 1?

Answer

OMG Ponies picture OMG Ponies · Sep 21, 2010

MySQL, SQL Server, PostgreSQL, SQLite support using the percent sign as the modulus:

WHERE column % 2 = 1

For Oracle, you have to use the MOD function:

WHERE MOD(column, 2) = 1