I had a situation where an XOR implementation was needed in a SELECT query. Since Oracle 10g doesnt have this operator predefined i did some gymnastics and ended up with this solution.
I decided to share this (among others) since human knowledge belongs to mankind
1. XOR in oracle
Example 1 - chars only
Eg we want to display results where ismale='1' XOR iscitizen='1' (ie either but not both)
Solution
SELECT ismale, iscitizen, (ismale + iscitizen) as xor
FROM tablename
WHERE ismale + iscitizen = 1;
NB: oracle will add(mathematical) chars as it would integers
Example 2 - varchars
Eg we want to display results where gender='male' XOR countryid='KE' (ie either but not both)
Solution
SELECT gender, countryid, (decode(gender,'male','1','0') + decode(countryid,'KE','1','0')) as xor
FROM tablename
WHERE (decode(gender,'male','1','0') + decode(countryid,'KE','1','0')) = 1;
NB: oracle will add chars as it would integers
No comments:
Post a Comment
Feel free to leave a comment