52

I am passing a simple query where I am searching for specific rows where OrderID is an even number

SELECT * 
FROM Orders 
WHERE mod(OrderID,2) = 0;

Error :

Syntax error (missing operator) in query expression 'mod(OrderID,2) = 0'.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
solanki kaushik
  • 521
  • 1
  • 4
  • 5

10 Answers10

95

You are not using Oracle, so you should be using the modulus operator:

SELECT * FROM Orders where OrderID % 2 = 0;

The MOD() function exists in Oracle, which is the source of your confusion.

Have a look at this SO question which discusses your problem.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
14
SELECT * FROM Orders where OrderID % 2 = 0;///this is for even numbers

SELECT * FROM Orders where OrderID % 2 != 0;///this is for odd numbers
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
8

Sql Server we can use %

select * from orders where ID % 2 = 0;

This can be used in both Mysql and oracle. It is more affection to use mod function that %.

select * from orders where mod(ID,2) = 0
Arun Solomon
  • 421
  • 3
  • 13
6

MOD() function exists in both Oracle and MySQL, but not in SQL Server.

In SQL Server, try this:

 SELECT * FROM Orders where OrderID % 2 = 0;
mxuanlin
  • 821
  • 6
  • 5
5

--This is for oracle

SELECT DISTINCT City FROM Station WHERE MOD(Id,2) = 0 ORDER BY City;
prashanth
  • 65
  • 1
  • 4
5

Try this:

SELECT DISTINCT city FROM STATION WHERE ID%2=0 ORDER BY CITY;
Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36
Sonu kumar
  • 51
  • 1
  • 1
1

For SQL Server:

SELECT * FROM Orders where OrderID % 2 = 0; //this is for even numbers

SELECT * FROM Orders where OrderID % 2 != 0; //this is for odd numbers

For Oracle and MySQL, you have to use the MOD function:

select * from orders where mod(ID,2) = 0

0
SELECT * FROM ( SELECT *, Row_Number() 
OVER(ORDER BY country_gid) AS sdfg  FROM eka_mst_tcountry ) t 
WHERE t.country_gid % 2 = 0 
slfan
  • 8,950
  • 115
  • 65
  • 78
0

In SQL, all these options can be used for MOD:

SELECT * FROM CITY WHERE MOD(ID,2) = 0 ORDER BY CITY;

OR

SELECT * FROM CITY WHERE ID % 2 = 0 ORDER BY CITY;

OR

SELECT * FROM CITY WHERE ID MOD 2 = 0 ORDER BY CITY;

Hope it helps!

Shweta
  • 661
  • 6
  • 11
0
;With MYCTE(Empid)
As 
(
  SELECT * FROM employeeodd 
)
Select *
From MYCTE
where empid % 2 = 0;

;With MYCTE(Empid)
As 
(
  SELECT * FROM employeeodd 
)
Select *
From MYCTE
where empid % 2! = 0;
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77