Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

using XOR like condition in where clause

TCSBPMUSERFeb 1 2012 — edited Feb 1 2012
Hi,
I need to use an XOR like condition in my where clause. please suggest some way.
Regards,
Ankit

Comments

Satish Kandi
f. ex.

WHERE (ColumnA = 0 AND columnB = 1) OR (ColumnA = 1 AND ColumnB = 0)

?
Nikolay Savvinov
Hi,

a XOR is simply (A OR B) AND NOT (A AND B). You can either use this definition directly in the WHERE clause or put it in a user-defined boolean function. There is also a discussion of XOR functions at asktom site:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1284001741333

Hope this helps.

Best regards,
Nikolay
Girish Sharma
XOR means "one or the other but not both". More defination : http://en.wikipedia.org/wiki/XOR

Something like this :
There is no general xor operator in Oracle. We applies XOR operator by using something like this :
SQL> select * from test;

         A          B
---------- ----------
         5          6
         5          5
         6          6
         6          5
         3          5
         5          3


SQL> select * from test where (a=5 or b=5) and not (a=5 and b=5);

         A          B
---------- ----------
         5          6
         6          5
         3          5
         5          3
Means, select all rows where a or b = 5, but both should not = 5.

Other interested threads may be :
4166189
1511281

Regards
Girish Sharma
Nicolas Gasparotto
Thread moved from Database General to SQL and PL/SQL forum.
Please, bear in mind to ask SQL question in SQL forum :
3077

Nicolas.
Frank Kulash
Hi, Ankit,

Here's another way, which might be more efficient and easier to debug, especially when the conditions are complicated, since it only needs to evaluate each condition once:.

To find employees in the scott.emp table whose job is 'CLERK', or whose salary is less than 1275, but not both:
SELECT	ename
,	job
,	sal
FROM	scott.emp
WHERE	CASE WHEN job = 'CLERK' THEN 1 ELSE 0 END 
      + CASE WHEN sal < 1275    THEN 1 ELSE 0 END	= 1
;
Output:
ENAME      JOB              SAL
---------- --------- ----------
WARD       SALESMAN        1250
MARTIN     SALESMAN        1250
MILLER     CLERK           1300
unknown-7404
Nice! Much easier to understand.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 29 2012
Added on Feb 1 2012
6 comments
29,967 views