Berra2k
Berra2k

Reputation: 328

SQL WHERE clauses not working

I have the below SQL script. It is very simple and unfortunately only the where clause that determines which campaigns are shown is working.

SELECT
    a.id,
    a.leadid,
    a.campaign_name__c,
    cast(a.firstrespondeddate as date),
    b.status,
    cast(b.createddate as date),
    b.ownerid
FROM rjm_current.sf_campaignmember a 
    LEFT JOIN rjm_current.sf_lead b ON a.leadid = b.id
WHERE a.campaign_name__c LIKE '%demo%' OR a.campaign_name__c LIKE '%contact%'
    AND cast(a.firstrespondeddate as date) <> '7/14/2015'
    AND b.status = 'New' 
    AND a.contactid IS NULL
    AND a.leadid IS NOT NULL

Any help greatly appreciated. In another query I had to convert the firstrespondeddate to date in one table and use another table to say firstrespondeddate <> '7/15/2015' but it should work without having to create a bunch of tables.

Upvotes: 1

Views: 2137

Answers (1)

klin
klin

Reputation: 121604

OR expression should be placed in brackets:

WHERE (a.campaign_name__c like '%demo%' OR a.campaign_name__c like '%contact%')
and cast(a.firstrespondeddate as date) <> '7/14/2015'
and b.status = 'New' 
and a.contactid is null
and a.leadid is not null

See Operator Precedence

Upvotes: 1

Related Questions