Question

Could someone please help me understand what each line in the WHERE clause of the SQL statement means? There are two parameters that can either be null or a valid date. How does the where clause work? Thank you in advance!

SELECT * from t1

WHERE

((@StartDate IS NULL AND @EndDate IS NULL)
OR (t1.date >= @StartDate AND t1.date <= @EndDate)
OR (t1.date >= @StartDate AND @EndDate IS NULL)
OR (@StartDate IS NULL AND t1.date <= @EndDate))
Was it helpful?

Solution

Return all entries if where is no range provided at all

((@StartDate IS NULL AND @EndDate IS NULL)

OR there is a date range and t1.date lies within that range

OR (t1.date >= @StartDate AND t1.date <= @EndDate)

OR the date range has no end and t1.date is after the start

OR (t1.date >= @StartDate AND @EndDate IS NULL)

OR the date range has no start and t1.date is before the range's end

OR (@StartDate IS NULL AND t1.date <= @EndDate))

In simple words select all entries if no range is given or select only those entries that are within the given range while allowing open ranges.


As for the example in your comment:

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

Your question is:

If SomeParameter was equal to 'N', then "and t1.name IN (Name)" would get appended to the where clause, else If SomeParameter was equal to 'Y', then "and t1.name IN (Name) or t1.name is null))" would get appended to the where clause. Is that correct?

No. The WHERE clause is always what it is. It is not changed, but it is evaluated. Let's see how it works for both cases:

Case 1: SomeParameter = 'N'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(TRUE and t1.name IN (Name)) OR 
(FALSE and t1.name IN (Name) OR t1.name is null)

If t1.name in (Name) is also true, the record is included in the result set, because

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE

If t1.name in (Name) is false, the record is not included in the result set, because

(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE

Case 2: SomeParameter = 'Y'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(FALSE and t1.name IN (Name)) OR 
(TRUE and t1.name IN (Name) OR t1.name is null)

If t1.Name in (Name) is also true, the record is included in the result set, because

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE

If t1.Name in (Name) is false, the record is not included in the result set, because

(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top