[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Retrieving SQL Records between dates with Delphi ADO

Posted on 2014-08-20
Medium Priority
Last Modified: 2015-03-05
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,
Question by:visionetv
LVL 49

Expert Comment

ID: 40275295
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"

Author Comment

ID: 40275333
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
LVL 49

Expert Comment

ID: 40275344
:) 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

LVL 53

Expert Comment

by:Vitor Montalvão
ID: 40275665
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.
LVL 28

Accepted Solution

Sinisa Vuk earned 2000 total points
ID: 40275869
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


Author Comment

ID: 40278172
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question