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);

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,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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"
visionetvAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
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);

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.
Sinisa VukSoftware architectCommented:
This is how should be using parameters:
InvoiceQuery.SQL.Text :='SELECT SUM(FullPrice) FROM [TransactionEntry] WHERE TransactionTime BETWEEN :pFromDate AND :pToDate';
InvoiceQuery.Parameters.ParamByName('pFromDate').Value := DateOf(DatePicker1.Date);
InvoiceQuery.Parameters.ParamByName('pToDate').Value := DateOf(DatePicker2.Date);

Open in new window

note: parameter name should start with

Second solution is to use dynamic sql string. If you want to include date ToDate in records... use
InvoiceQuery.Parameters.ParamByName('pToDate').Value := DateOf(DatePicker2.Date)+1;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
visionetvAuthor Commented:
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;;

The above satisfies the Delphi compiler and the application now executes without a problem
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.