Please Visit Our New Website for Further Communication

Amazon

30 Jan 2013

SQL Query to Fetch Even/Odd values in Oracle

To fetch Even/Odd values from a numeric column in an Oracle Table:
Step 1: Create a Table “Test” as shown below:
CREATE TABLE TEST(A NUMBER);
Step 2: Insert few random numeric values into “TEST” Table.
INSERT INTO TEST VALUES(1);
INSERT INTO TEST VALUES(15);
INSERT INTO TEST VALUES(4);
INSERT INTO TEST VALUES(26);
INSERT INTO TEST VALUES(497);
INSERT INTO TEST VALUES(590);
INSERT INTO TEST VALUES(9826);
INSERT INTO TEST VALUES(1003);
INSERT INTO TEST VALUES(333);
Step 3: Select the “TEST” Table.
SELECT * FROM TEST;
A
1
15
4
26
497
590
9826
1003
333








Step 4: Run the below query to fetch “EVEN” values from above Table column.
SELECT * FROM TEST WHERE MOD(A,2)=0;
Output:
A
4
26
590
9826


Step 5: Run the below query to fetch “ODD” values from above Table column.
SELECT * FROM TEST WHERE MOD(A,2)!=0;
Output:
A
1
15
497
1003
333


No comments:

Post a Comment