Click here to Skip to main content
15,900,616 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want all the contactids EXCEPT where OptOut or Exception field is True.
For instance : If OptOut of ContactID:3 is T than Query will fetch ContactID 1 and 2.

ContactID OptIN OptOut Exception
1 T NULL NULL
2 T NULL NULL
3 F T NULL

I m using following query but I guess missing some logic in AND/OR Condition.

SQL
SELECT
 A.ContactID,
 B.CASLOptIN,
 B.CASLOptOut,
 B.CASLException
FROM
 Contact A INNER JOIN GE_ContactInfo B
  ON A.CONTACTID=B.CONTACTID
WHERE
 A.Accountid ='XXXXXXXXX'
AND
(
 (B.CASLOptOut ='F' or B.CASLOptOut = null)
OR
 (B.CASLEXCEPTION ='F' or B.CASLEXCEPTION = null)
)



Not Null Condition is not working....dont knw where I missing silly logic.
Please any help will be appreciable.
Thanks.
Posted

Hi,

Check this...


SQL
SELECT
 A.ContactID,
 B.CASLOptIN,
 B.CASLOptOut,
 B.CASLException
FROM
 Contact A INNER JOIN GE_ContactInfo B
  ON A.CONTACTID=B.CONTACTID
WHERE
 A.Accountid ='XXXXXXXXX'
AND
( ISNULL(B.CASLOptOut,'F')='F' OR ISNULL(B.CASLEXCEPTION ,'F')='F' )



Hope this will help you.

Cheers
 
Share this answer
 
Comments
lovejeet0707 9-Sep-14 8:40am    
Not Working :(
Magic Wonder 9-Sep-14 8:49am    
Can you provide sample data? What is the output you are getting?
Hi,

I hope using COALESCE will work.
Check this Demo[^]


:-)
 
Share this answer
 
Your query is currently returning records where either field is false or null, because you've used an OR to combine the conditions. Try:
SQL
WHERE
   A.Accountid ='XXXXXXXXX'
AND
   (B.CASLOptOut ='F' Or B.CASLOptOut = null)
AND
   (B.CASLEXCEPTION ='F' Or B.CASLEXCEPTION = null)
 
Share this answer
 
Comments
lovejeet0707 9-Sep-14 8:38am    
I guess it will be OR.
bcz i need either of Optout or exception NOT both....i.e even if One succeed it will work.
Richard Deeming 9-Sep-14 8:40am    
That's not the question you asked. You said you want to exclude records where either CASLOptOut or CASLException are true. The query you posted only excludes records where both columns are true.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900