Solved

# SQL: AND or OR logic

Posted on 2014-10-21
114 Views
I have a stored procedure where @DateRangeTypeCancellation is either true or false.

When true, I need to return records where:
if ( CA.CancellationEffectiveDate < PolCancelDate.MinCancelDate ) then CA.CancellationEffectiveDate between dates

if ( CA.CancellationEffectiveDate > PolCancelDate.MinCancelDate ) then PolCancelDate.MinCancelDate between dates

I think the below code does that, but I'm not sure if the "AND" on line 5 should be an OR?

``````WHERE
(
@DateRangeTypeCancellation = 0 OR
((CA.CancellationEffectiveDate < PolCancelDate.MinCancelDate AND CA.CancellationEffectiveDate BETWEEN @startDate and @endDate)
AND
(CA.CancellationEffectiveDate > PolCancelDate.MinCancelDate AND PolCancelDate.MinCancelDate BETWEEN @startDate and @endDate))
)
``````
0
Question by:pzozulka

LVL 39

Expert Comment

ID: 40395314
You could also do it in two separate SELECT queries and use UNION (for distinct rows) or UNION ALL between the two SELECT statements where you can have different WHERE clause in each.
0

LVL 65

Accepted Solution

Jim Horn earned 500 total points
ID: 40395322
>if ( CA.CancellationEffectiveDate < PolCancelDate.MinCancelDate ) then CA.CancellationEffectiveDate between dates
>if ( CA.CancellationEffectiveDate > PolCancelDate.MinCancelDate ) then PolCancelDate.MinCancelDate between dates
Since only one of the above can exists at the same time, it would be OR.
Also, is there an = that needs to be handled?
Also, might want to add Line 3 of the below
``````WHERE (
@DateRangeTypeCancellation = 0 OR
(@DateRangeTypeCancellation = 1 AND (
(CA.CancellationEffectiveDate < PolCancelDate.MinCancelDate AND CA.CancellationEffectiveDate BETWEEN @startDate and @endDate)
OR
(CA.CancellationEffectiveDate > PolCancelDate.MinCancelDate AND PolCancelDate.MinCancelDate BETWEEN @startDate and @endDate))
``````
)
0

LVL 8

Author Comment

ID: 40395350
Jim: I don't know if line 3 in your code is required because the OR on line 2 would implicitly make that happen.
In other words, if @DateRangeTypeCancellation is set to 0, then the following: @DateRangeTypeCancellation = 0
will evaluate to TRUE, and the rest will get skipped because of the OR.

However, if @DateRangeTypeCancellation is set to 1, then the following: @DateRangeTypeCancellation = 0
will evaluate to FALSE, and the rest of the expression will be evaluated because of the OR.
0

LVL 11

Expert Comment

ID: 40395821
``````WHERE	@DateRangeTypeCancellation = 0
OR	(	@DateRangeTypeCancellation = 1
AND	CASE	WHEN CA.CancellationEffectiveDate < PolCancelDate.MinCancelDate
THEN CA.CancellationEffectiveDate
ELSE PolCancelDate.MinCancelDate
END BETWEEN @startDate and @endDate
)
``````
0

## Featured Post

### Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!