Solved

SQL: AND or OR logic

Posted on 2014-10-21
4
125 Views
Last Modified: 2014-10-22
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))
)

Open in new window

0
Comment
Question by:pzozulka
4 Comments
 
LVL 39

Expert Comment

by:lcohan
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

by:
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))

Open in new window

)
0
 
LVL 8

Author Comment

by:pzozulka
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

by:John_Vidmar
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
	)

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 59
SQL, add where clause 5 23
SSRS: Why is Visual Studio stripping these properties? 2 21
SQL Database Restore 2008 R2 1 12
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

860 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