Microsoft
Software
Hardware
Network
Question : TRANSACT SQL: COALESCE FUNCTION - DATE RANGES AND NULL VALUES
I am using Transact SQL to process a series of possible selection filters from by calling application in VB6. If nothing is selected for a given field in the calling app, then I don't want to filter on that field. The COALESCE function works well for this, and is working on all but the Date Range. All the possible fields in the database either contain a value, or a NULL value. The problem I am having is with the Date Range. If I pass in a range of dates, it will filter on that range and return only those records in that range. If I pass in nothing (no filter wanted) I set the Date Range variables to NULL (as I do for the other fields) and would expect all records to be returned whether it has a date or a NULL. Instead, it is returning all records with any date value in the field - it is excluding the Date records with a value of NULL in the database. I'm not understanding why it works as expected with this approach for the other selection fields, yet not for the Date. My query is below. With its current variable settings I would expect it to return ALL records.
--Apply filters as needed based on values passed in from calling application.
--Use COALESCE function: If no value is passed in, set variable to NULL and then COALESCE will not apply a filter.
--this works fine, filtering as needed or returning all records as needed (whether or not there is a non-NULL value in the field), except for the DATE field referenced here
--For Date range, if I pass in a range it works correctly and filters on range, but if I SET Date Range variables to NULL (as I do the other variables) so as not to filter on the field, it
--returns only records with a date value in the field. It does not return the records with a NULL value.
DECLARE @TERRITORY CHAR(5)
SET @TERRITORY=NULL
DECLARE @STATE VARCHAR(10)
SET @STATE=NULL
DECLARE @DATEBEGIN DATETIME
SET @DATEBEGIN=NULL
DECLARE @DATEEND DATETIME
SET @DATEEND=NULL
SELECT COMPANY, ADDRESS, CITY, STATE, ZIPCODE, TERRITORY, FOLLOWUPDATE
FROM LEADS
WHERE
TERRITORY = COALESCE(@TERRITORY, TERRITORY) AND
STATE = COALESCE(@STATE, STATE) AND
FOLLOWUPDATE BETWEEN COALESCE(@DATEBEGIN, FOLLOWUPDATE) AND COALESCE(@DATEEND, FOLLOWUPDATE)
Answer : TRANSACT SQL: COALESCE FUNCTION - DATE RANGES AND NULL VALUES
You might just need to change this
FOLLOWUPDATE BETWEEN COALESCE(@DATEBEGIN, FOLLOWUPDATE) AND COALESCE(@DATEEND, FOLLOWUPDATE)
to
(FOLLOWUPDATE >= @DATEBEGIN OR @DATEBEGIN IS NULL) AND (FOLLOWUPDATE <= @DATEEND OR @DATEEND IS NULL)
Random Solutions
Copy data from sheets according to sub header.
trying to add PasswordChar to text box Web c#
Can't get an ASP page to display using IIS 7
Increasing a value in an access table
Two Account only one PST, Outlook 2010
Windows Fax and Scan
how to see mails of all users in Exchange server 2007 & Exchange 2003 by an Admin
MySQL php Select rows similar price
"HTTP Error 404.0 - Not Found" in ASP.Net
Jquery