59

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?

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
Carlos
  • 681
  • 2
  • 6
  • 4

4 Answers4

86

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
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
20

At least some versions of SQL (Oracle, Informix, DB2, ISO Standard) support:

WHERE MOD(value, 2) = 1

MySQL supports '%' as the modulus operator:

WHERE value % 2 = 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

Note: Disregard this answer, as I must have misunderstood the question.

select *
  from Table
  where len(ColName) mod 2 = 1

The exact syntax depends on what flavor of SQL you're using.

David R Tribble
  • 11,918
  • 5
  • 42
  • 52
1

select * from table where value % 2 = 1 works fine in mysql.

einarmagnus
  • 3,507
  • 1
  • 21
  • 31