Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL - Show offers between range
    primarykey
    data
    text
    <p>I am currently working on constructing a SQL query around offers.</p> <p>Basically an offer has a begin and end date.</p> <p>Scenario 1: The user only specifies a date from.</p> <p>Solution 1: All offers are display which start on or past that day.</p> <p>Scenario 2: The user only specifies a TO date.</p> <p>Solution 2: All offers are display which end on or before that given date.</p> <p>Scenario 3: The user specifies both a TO and FROM date to search.</p> <p>The issue with solution 3 is as follows.</p> <p>OFFER - from 01-01-2012 to 03-03-2012</p> <p>SEARCH - from 01-01-2012 to the 02-02-2012</p> <p>The offer should return in the query as it falls between the two search values.</p> <p>My current query is below, however its not working as required.</p> <pre><code>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 &gt;= @from_date OR @from_date IS NULL) AND (reco.DateExpires &lt;= @to_date OR @to_date IS NULL) ORDER BY retr.Name END GO </code></pre> <p>Please note scenarios 1 &amp; 2 are covered by the query above its 3 which is causing a problem.</p> <p>Steven</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload