0

What I thought was simple turned out to be a nightmare . . . The table I am trying to get the data out of is very simple:

R_TYPE (string)
R_PRICE (currency)
S_DATE (date)
E_DATE (date)

I run:

SELECT * 
  FROM MYTABLE 
 WHERE R_TYPE = :a1 
   AND R_PRICE BETWEEN S_DATE = :a2 
                   AND E_DATE = :a3

Parameters are:

ABSQuery1.params.ParamByName('a1').asString :=cxTextEdit;
ABSQuery1.params.ParamByName('a2').asDate := DateTimePicker1;
ABSQuery1.params.ParamByName('a3').asDate := DateTimePicker2;

But it wont work.... Any ideas?

1
  • 1
    Your SQL is obviously logically flawed, because it boils down to "Give me all the records of type "a1" that have a price between True and False". Would you mind describing in plain English what you'd like to achieve? Jun 9, 2011 at 7:08

1 Answer 1

1

Your query is muddled: Price can't be between the boolean result of date comparisons.

If the date picker is picking any price that had its entire duration (ie it started and ended) between the dates picked, use this:

select * from MYTABLE where R_TYPE = :a1 and S_DATE > :a2 AND E_DATE < :a3;

If the date picker is picking any price that had some duration between the dates picked (ie there was at least some overlap of the date range picked and the price's date range), use this:

select * from MYTABLE where R_TYPE = :a1 and S_DATE < :a3 AND E_DATE > :a2;
7
  • What about S_DATE and E_DATE equal to the parameters? BETWEEN works inclusively. Plus in the first query you could simply use BETWEEN twice, once for each parameter. Jun 9, 2011 at 5:52
  • 'select * from R_PRICES where R_TYPE = :A1 and S_DATE < :a3 AND E_DATE > :a2 '; is picking only the designed 'price range'. Example: PRICE region between 25/4/2011 and 25/6/2011 is 120 Euro's. Now when i hit the quoted queery with parameters: :a2 = 10/6/2011 and :a3 = 11.6.2011 I get in the grid: S_DATE =25/4/2011 E_DATE = 25/6/2011 Which is not what I want.I want 'selected' dates displayed and the right price i.e S_DATE = 10/6/2011 E_DATE = 11.6.2011 PRICE 120 first version of the queery : and S_DATE > :a2 AND E_DATE < :a3 run with same parameters does not display anything (?).
    – user763539
    Jun 10, 2011 at 17:32
  • I think this query will need a DATEDIFF parameter (AS DAYS) too because I think I will run in overlapping prices if they span over different price ranges. Something similar here : stackoverflow.com/questions/5452774/…
    – user763539
    Jun 10, 2011 at 17:32
  • The result I am trying to achieve is this : (THIS IS TABLE DATA) S_DATE E_DATE PRICE 25/4/2011 25/6/2011 120 25/6/2011 20/8/2011 140 Now,if I select date parameters in my datetimepickers like :a2 = 24.6.2011 :a3 = 28.6.2011 In the grid I get : S_DATE E_DATE PRICE DAYS 24.6.2011 25/6/201 120 1 25/6/201 28.6.2011 140 3
    – user763539
    Jun 10, 2011 at 17:33
  • stackoverflow.com/questions/5452774/… This is more or less what I need just with parameters. i restructured table and queery but cant make it work - code above
    – user763539
    Jun 11, 2011 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.