WindowMan
WindowMan

Reputation: 21

SQL adding a date range help please

I'm using

  Select Customer.QuoteID, Quote.QuoteID
    From Customer, Quote
   Where Customer.QuoteID=Quote.QuoteID
Order By Quote.QuoteID

I'd like to then specify a date range (Quote.CreationDate) or at least show dates greater than 31/03/2013, but I am not sure how to add this?

I am hoping to use a dialog box in Delphi to select the date range eventually...

Upvotes: 0

Views: 237

Answers (2)

mickfold
mickfold

Reputation: 2003

To limit by date range you could do something like the following:

Select Customer.QuoteID, Quote.QuoteID 
From Customer, Quote 
Where Customer.QuoteID = Quote.QuoteID 
  and Quote.CreationDate between :date_from and :date_to
Order By Quote.QuoteID

where :date_from and :date_to are your date parameters

Upvotes: 6

Jeff Rosenberg
Jeff Rosenberg

Reputation: 3572

You can just add these to the where clause:

Select Customer.QuoteID, Quote.QuoteID
From Customer, Quote
Where Customer.QuoteID=Quote.QuoteID
  And Quote.CreationDate > '2013-03-31'
Order By Quote.QuoteID

Here it is with standard ANSI join syntax, which is generally preferred and which I find a bit easier to read in this case:

Select Customer.QuoteID, Quote.QuoteID
From Customer
Inner Join Quote On Customer.QuoteID = Quote.QuoteID
Where Quote.CreationDate > '2013-03-31'
Order By Quote.QuoteID

Or if you're looking for a range between two dates:

Select Customer.QuoteID, Quote.QuoteID
From Customer
Inner Join Quote On Customer.QuoteID = Quote.QuoteID
Where Quote.CreationDate >= '2013-03-31' 
  AND Quote.CreationDate <= '2013-04-02'
Order By Quote.QuoteID

Finally, on certain RDBMS platforms, you can use the BETWEEN operator as well, but the syntax in my last example will always work.

Upvotes: 2

Related Questions