Fetching Even and Odd Records in Oracle

This tip will help you understand how to apply the mod function and use it to obtain even and odd records from columns in Oracle.

You basically want to retrieve records in odd and even order based on a column. So, suppose you want to fetch all records with the its value as an odd: (1, 3, 5, 7…). You’d use this line to form the result:

SELECT * FROM TABLENAME WHERE MOD(column_choosen_with_integer_values,2) = 1;

For even records, you’d use:

SELECT * FROM TABLENAME WHERE MOD(column_choosen_with_integer_values,2) = 0;

You can use this same logic to fetch odd and even records in a table using ROWNUM as well.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: