Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Rows with id having even number

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'.

like image 899
solanki kaushik Avatar asked Jan 31 '16 06:01

solanki kaushik


People also ask

How do you select an even number ID in SQL?

How do you check if a number is even in SQL Server? 10 Answers Use the modulus operator n % 2 . It returns 0 if the number is even, and 1 if the number is odd.

How do you select rows with the same value?

To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.

How can I get odd ID in SQL?

To find and return the records with the odd or even values, the most simple way is to check the remainder when we divide the column value by 2. When the remainder is 0, that's an even number, otherwise, that's an odd number.


3 Answers

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.

like image 200
Tim Biegeleisen Avatar answered Sep 21 '22 05:09

Tim Biegeleisen


SELECT * FROM Orders where OrderID % 2 = 0;///this is for even numbers  SELECT * FROM Orders where OrderID % 2 != 0;///this is for odd numbers 
like image 37
dotnet technologies Avatar answered Sep 19 '22 05:09

dotnet technologies


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
like image 25
Arun Solomon Avatar answered Sep 18 '22 05:09

Arun Solomon