Link to home
Start Free TrialLog in
Avatar of visionetv
visionetvFlag for United States of America

asked on

Retrieving SQL Records between dates with Delphi ADO

I have assembled from researching the Web what I thought was the best solution for retrieving records from a SQL 2008/R2 database using two Delphi DateTime pickers. The code is as follows;

InvoiceQuery.SQL.Text :='SELECT SUM(FullPrice)FROM [TransactionEntry] WHERE TransactionTime BETWEEN DatePicker1.Date AND DatePicker2.Date';
InvoiceQuery.Parameters.ParamByName('FromDate').Value := DateOf(DatePicker1.Date);
InvoiceQuery.Parameters.ParamByName('ToDate').Value := DateOf(DatePicker2.Date);
InvoiceQuery.Open;

Running the code I get an EOLexeception message; "Parameter Object is improperly defined. Inconsistent or incomplete information was provided."

I've tried all of the 'Name - Value' combinations I can think of in the Parameters editor for the Delphi ADOQuery component which the same result, any help will be greatly appreciated

Thank you,
Visionetv
Avatar of PortletPaul
PortletPaul
Flag of Australia image

I can't comment on the Delphi ADO aspect of your question - sorry.

But: Transactional data typically carries accurate date/time references

For example a transaction of $100,000 could occur at: 2014-07-31 19:21:37 +56782

If your users choose 2 dates, let's say July 1 and July 31 2014, your sql would end-up looking like this:

SELECT SUM(FullPrice)FROM [TransactionEntry]
WHERE TransactionTime BETWEEN '2014-07-01' AND '2014-07-31'

Guess what? That transaction of $100,000 would NOT be included by that query.

please see: "Beware of Between"
Avatar of visionetv

ASKER

I read your article great information to have. In the instance of this application the record retrieval is from a date variable only. As to the Delphi issue I'm still researching the runtime error. The SQL solution seemed pretty straight forward, may need an alternative.

Thanks for the response
:) thanks. Good luck on the Delphi aspect.

It is good to recognize that your transactions are "whole dates" and what that means for using between. But if for any reason they started to be more precise than that, then use of between would introduce problems. For a few extra characters of code, you can use a safe/reliable date range method in all situations, regardless of stored date/time precision.

Cheers, Paul
I also can't help you with Delphi but for sure SQL Server doesn't know what are "DatePicker1.Date AND DatePicker2.Date"

And your parameters name are "FromDate" and "ToDate", as you post:
InvoiceQuery.Parameters.ParamByName('FromDate').Value := DateOf(DatePicker1.Date);
 InvoiceQuery.Parameters.ParamByName('ToDate').Value := DateOf(DatePicker2.Date);

You can also use dynamic query instead of parameters:

InvoiceQuery.SQL.Text :='SELECT SUM(FullPrice) FROM [TransactionEntry] WHERE TransactionTime BETWEEN ' + DateOf(DatePicker1.Date) + ' AND ' + DateOf(DatePicker2.Date);
 InvoiceQuery.Open;

Open in new window


NOTE: Since I don't know Delphi the code may need some modifications to work but wanted to give a possible way for querying without using parameters.
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all for your comments... the following is the code I re-drafted to solve the problem:

InvoiceQuery1.SQL.Add('SELECT SUM(FullPrice) AS Cash');
InvoiceQuery1.SQL.Add('FROM TransactionEntry');
InvoiceQuery1.SQL.Add('WHERE TransactionTime BETWEEN :MIN_DATE AND :MAX_DATE');
InvoiceQuery1.Parameters.ParamByName('MIN_DATE').Value := DatePicker1.Date;
InvoiceQuery1.Parameters.ParamByName('MAX_DATE').Value := DatePicker2.Date;;
InvoiceQuery1.Open;

The above satisfies the Delphi compiler and the application now executes without a problem