Solved

pl/sql exclusive OR

Posted on 2007-11-15
5
5,670 Views
Last Modified: 2013-12-07
How do you code an exclusive OR in PL SQL.
ie: one or other but not both. Is there an operator to do this?
ie: if both are true then return false
0
Comment
Question by:claghorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:bobbymanocha
ID: 20289580
Here's one way:

create or replace function f_xor(
  condition1 boolean,
  condition2 boolean) return boolean
as
begin
  return (condition1 and condition2);
end;
/
0
 
LVL 1

Expert Comment

by:bobbymanocha
ID: 20289649
I posted the wrong code earlier.  Sorry about that.

create or replace function f
(condition1 boolean, condition2 boolean) return boolean as  
begin
return XOR(condition1, condition2);
end;
/
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 20296637
another sample


DECLARE
  a BOOLEAN;
  b BOOLEAN;
 
  
  FUNCTION exclusive_or(c1 IN BOOLEAN, c2 IN BOOLEAN)
  RETURN BOOLEAN
  IS 
  BEGIN
    RETURN ((c1 OR c2) AND NOT (c1 AND c2));
  END;
  
  
BEGIN
  
  a := TRUE;
  b := TRUE;
    
  IF exclusive_or(a,b) THEN
    DBMS_OUTPUT.PUT_LINE('Both TRUE results in TRUE');
  ELSE
    DBMS_OUTPUT.PUT_LINE('Both TRUE results in FALSE');
  END IF;
  
  a := FALSE;
  b := TRUE;
    
  IF exclusive_or(a,b) THEN
    DBMS_OUTPUT.PUT_LINE('First FALSE, second TRUE results in TRUE');
  ELSE
    DBMS_OUTPUT.PUT_LINE('First FALSE, second TRUE results in FALSE');
  END IF;
 
  a := TRUE;
  b := FALSE;
    
  IF exclusive_or(a,b) THEN
    DBMS_OUTPUT.PUT_LINE('First TRUE, second FALSE results in TRUE');
  ELSE
    DBMS_OUTPUT.PUT_LINE('First TRUE, second FALSE results in FALSE');
  END IF;
 
  a := FALSE;
  b := FALSE;
    
  IF exclusive_or(a,b) THEN
    DBMS_OUTPUT.PUT_LINE('Both FALSE results in TRUE');
  ELSE
    DBMS_OUTPUT.PUT_LINE('Both FALSE results in FALSE');
  END IF;
 
END;  
  

Open in new window

0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 20297887
( c1 and not c2 ) or ( c2 and not c1 )  --> i think this should be fine as well.

Thanks
0
 

Author Comment

by:claghorn
ID: 20298518
OK, thank you all, I will assess which one works for me tonight and release the points.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error querying database link from Oracle 10g to Postgresql 7 100
add more rows to hierarchy 3 35
Read XML values 8 56
Pivoting oracle table 9 73
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question