i have a table MEN in sql server 2008 that contain 150 rows.
how i can show only the even or only the odd rows ?
thank's in advance
i have a table MEN in sql server 2008 that contain 150 rows.
how i can show only the even or only the odd rows ?
thank's in advance
Check out ROW_NUMBER()
SELECT t.First, t.Last
FROM (
SELECT *, Row_Number() OVER(ORDER BY First, Last) AS RowNumber
--Row_Number() starts with 1
FROM Table1
) t
WHERE t.RowNumber % 2 = 0 --Even
--WHERE t.RowNumber % 2 = 1 --Odd
Assuming your table has auto-numbered field "RowID" and you want to select only records where RowID is even or odd.
To show odd:
Select * from MEN where (RowID % 2) = 1
To show even:
Select * from MEN where (RowID % 2) = 0
FASTER: Bitwise instead of modulus.
select * from MEN where (id&1)=0;
Random question: Do you actually use uppercase table names? Usually uppercase is reserved for keywords. (By convention)
odd number query:
SELECT *
FROM ( SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,2) = 1
even number query:
SELECT *
FROM ( SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,3) = 0
Try this :
odd :
select * from(
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber',
FROM table1
) d where (RowNumber % 2) = 1
even :
select * from(
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber',
FROM table1
) d where (RowNumber % 2) = 0
SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM empsal) a WHERE (row_number%2) = 1
and
SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM empsal) a WHERE (row_number%2) = 0
For even values record :
select * from www where mod(salary,2)=0;
For odd values record:
select * from www where mod(salary,2)!=0;
SELECT *
FROM
(
SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,2) = 1
select * from Tablename
where id%2=0
Following is for fetching even number:: Select * from MEN where Men_ID%2=0;
Following is for fetching odd number:: Select * from MEN where Men_ID%2!=0;
Here MEN is your table_name Men_ID is the column in MEN Table.
Here’s a simple and straightforward answer to your question, (I think). I am using the TSQL2012 sample database and I am returning only even or odd rows based on “employeeID” in the “HR.Employees” table.
USE TSQL2012;
GO
Return only Even numbers of the employeeID:
SELECT *
FROM HR.Employees
WHERE (empid % 2) = 0;
GO
Return only Odd numbers of the employeeID:
SELECT *
FROM HR.Employees
WHERE (empid % 2) = 1;
GO
Hopefully, that’s the answer you were looking for.
To fetch even records
select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=0;
To fetch odd records
select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=1;
ODD ROWS
select * from (select mod(rownum,2) as num , employees.* from employees) where num =0;
EVEN ROWS
select * from (select mod(rownum,2) as num , employees.* from employees) where num =1;
To select an odd id from a table:
select * from Table_Name where id%2=1;
To select an even id from a table:
select * from Table_Name where id%2=0;
Try following
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
for SQL > odd:
select * from id in(select id from employee where id%2=1)
for SQL > Even:
select * from id in(select id from employee where id%2=0).....f5