I am currently working on constructing a SQL query around offers.
Basically an offer has a begin and end date.
Scenario 1: The user only specifies a date from.
Solution 1: All offers are display which start on or past that day.
Scenario 2: The user only specifies a TO date.
Solution 2: All offers are display which end on or before that given date.
Scenario 3: The user specifies both a TO and FROM date to search.
The issue with solution 3 is as follows.
OFFER - from 01-01-2012 to 03-03-2012
SEARCH - from 01-01-2012 to the 02-02-2012
The offer should return in the query as it falls between the two search values.
My current query is below, however its not working as required.
CREATE PROCEDURE [dbo].[GetAllOffers]
@retailer_id BIGINT,
@opt_in BIGINT,
@use_once BIGINT,
@from_date DATETIME,
@to_date DATETIME
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
SET NOCOUNT ON;
SELECT retr.Name,
reco.Headline,
reco.isOptIn,
reco.isUseOnce,
reco.DateValidFrom,
reco.DateExpires,
reco.Id AS OfferId
FROM RetailerCoupon reco
INNER JOIN Retailer retr
ON reco.RetailerId = retr.Id
WHERE (reco.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reco.isOptIn = @opt_in
OR @opt_in IS NULL)
AND (reco.isUseOnce = @use_once
OR @use_once IS NULL)
AND (reco.DateValidFrom >= @from_date
OR @from_date IS NULL)
AND (reco.DateExpires <= @to_date
OR @to_date IS NULL)
ORDER BY retr.Name
END
GO
Please note scenarios 1 & 2 are covered by the query above its 3 which is causing a problem.
Steven