Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

SQL Server Reporting Services (SSRS) – Dealing with multiple date ranges with the option of choosing NULL for the start and/or end date in SSRS Reports

select * from date_range_table
where

((@startdate1 IS NULL AND @enddate1 IS NULL)
OR (start_date1 >= @startdate1 AND start_date1 <= @enddate1)
OR (start_date1 >= @startdate1 AND @enddate1 IS NULL)
OR (@startdate1 IS NULL AND start_date1 <= @enddate1))

AND

((@startdate2 IS NULL AND @enddate2 IS NULL)
OR (start_date2 >= @startdate2 AND start_date2 <= @enddate2)
OR (start_date2 >= @startdate2 AND @enddate2 IS NULL)
OR (@startdate2 IS NULL AND start_date2 <= @enddate2))

AND

((@startdate3 IS NULL AND @enddate3 IS NULL)
OR (start_date3 >= @startdate3 AND start_date3 <= @enddate3)
OR (start_date3 >= @startdate3 AND @enddate3 IS NULL)
OR (@startdate3 IS NULL AND start_date3 <= @enddate3))

OR IF YOU NEED TO CAST THE TIME DO THIS:

WHERE

((@startdate1 IS NULL AND @enddate1 IS NULL)
OR (start_date1 >= (@startdate1 + cast('00:00:00.000' as smalldatetime)) AND start_date1 <= (@enddate1 + cast('23:59:59.999' as smalldatetime)))
OR (start_date1 >= (@startdate1 + cast('00:00:00.000' as smalldatetime)) AND @enddate1 IS NULL)
OR (@startdate1 IS NULL AND start_date1 <= (@enddate1 + cast('23:59:59.999' as smalldatetime))))

AND

((@startdate2 IS NULL AND @enddate2 IS NULL)
OR (start_date2 >= (@startdate2 + cast('00:00:00.000' as smalldatetime)) AND start_date2 <= (@enddate2 + cast('23:59:59.999' as smalldatetime)))
OR (start_date2 >= (@startdate2 + cast('00:00:00.000' as smalldatetime)) AND @enddate2 IS NULL)
OR (@startdate2 IS NULL AND start_date2 <= (@enddate2 + cast('23:59:59.999' as smalldatetime))))

AND

((@startdate3 IS NULL AND @enddate3 IS NULL)
OR (start_date3 >= (@startdate3 + cast('00:00:00.000' as smalldatetime)) AND start_date3 <= (@enddate3 + cast('23:59:59.999' as smalldatetime)))
OR (start_date3 >= (@startdate3 + cast('00:00:00.000' as smalldatetime)) AND @enddate3 IS NULL)
OR (@startdate3 IS NULL AND start_date3 <= (@enddate3 + cast('23:59:59.999' as smalldatetime))))


I have found that a ‘CASE WHEN’ http://www.jamesandchey.net/?p=119 will work fine when working with one date range, but with multiple date ranges, the results can be unpredictable. The above logic proves to be a more effective way to get the results your are looking for.

This SQL says this:

If the @start date and @end date are NULL, get all rows. Basically ignore this where clause.

If your @start date is defined and @end date is defined, get all rows within the start and end date range

If your @start date is defined and @end date is NULL, the date range has no end, so get all rows after the range’s start.

If your @start date is NULL and @end date is defined, the date range has no start, so get all rows before the range’s end.

Leave a Reply