Select Rows with id having even number
Asked Answered
U

10

54

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

Unfold answered 31/1, 2016 at 6:20 Comment(7)
Oracle-Server or SQL-Server ?? @solankikaushikTeague
Doesn't look like T-SQL (MS SQL Server) to me - there's no mod function that I've heard of. OP could do SELECT * FROM Orders WHERE OrderID % 2 = 0 though, I think.Rilda
@Malik Asif : Sql ServerUnfold
Go to link: Already given answer OR Query is: SELECT * FROM Orders where OrderId % 2 = 0;Teague
@Rilda : How to perform this query in ms-sql server ?Unfold
works for me in sql plus serverAttaway
This will work in MySQLAmbie
A
96

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.

Audi answered 31/1, 2016 at 6:24 Comment(0)
I
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
Ingenuous answered 20/10, 2017 at 11:47 Comment(0)
R
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
Rigdon answered 29/11, 2018 at 13:37 Comment(1)
The question is about SQL Server, not Oracle or MySQL though.Lenardlenci
P
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;
Palaeogene answered 31/1, 2016 at 6:44 Comment(0)
F
5

--This is for oracle

SELECT DISTINCT City FROM Station WHERE MOD(Id,2) = 0 ORDER BY City;
Faultless answered 19/7, 2019 at 19:7 Comment(0)
C
5

Try this:

SELECT DISTINCT city FROM STATION WHERE ID%2=0 ORDER BY CITY;
Chatoyant answered 28/10, 2020 at 10:8 Comment(0)
J
2

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

Jillene answered 22/10, 2021 at 5:30 Comment(0)
O
1

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!

Okeefe answered 1/2, 2023 at 10:3 Comment(0)
S
1
;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;
Scientism answered 25/3, 2023 at 5:24 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OPEfrenefron
I
0
SELECT * FROM ( SELECT *, Row_Number() 
OVER(ORDER BY country_gid) AS sdfg  FROM eka_mst_tcountry ) t 
WHERE t.country_gid % 2 = 0 
Ingenuous answered 20/10, 2017 at 11:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.