visionetv
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.Pa ramByName( 'FromDate' ).Value := DateOf(DatePicker1.Date);
InvoiceQuery.Parameters.Pa ramByName( '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
InvoiceQuery.SQL.Text :='SELECT SUM(FullPrice)FROM [TransactionEntry] WHERE TransactionTime BETWEEN DatePicker1.Date AND DatePicker2.Date';
InvoiceQuery.Parameters.Pa
InvoiceQuery.Parameters.Pa
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
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 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
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.Pa ramByName( 'FromDate' ).Value := DateOf(DatePicker1.Date);
InvoiceQuery.Parameters.Pa ramByName( 'ToDate'). Value := DateOf(DatePicker2.Date);
You can also use dynamic query instead of parameters:
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.
And your parameters name are "FromDate" and "ToDate", as you post:
InvoiceQuery.Parameters.Pa
InvoiceQuery.Parameters.Pa
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your comments... the following is the code I re-drafted to solve the problem:
InvoiceQuery1.SQL.Add('SEL ECT SUM(FullPrice) AS Cash');
InvoiceQuery1.SQL.Add('FRO M TransactionEntry');
InvoiceQuery1.SQL.Add('WHE RE TransactionTime BETWEEN :MIN_DATE AND :MAX_DATE');
InvoiceQuery1.Parameters.P aramByName ('MIN_DATE ').Value := DatePicker1.Date;
InvoiceQuery1.Parameters.P aramByName ('MAX_DATE ').Value := DatePicker2.Date;;
InvoiceQuery1.Open;
The above satisfies the Delphi compiler and the application now executes without a problem
InvoiceQuery1.SQL.Add('SEL
InvoiceQuery1.SQL.Add('FRO
InvoiceQuery1.SQL.Add('WHE
InvoiceQuery1.Parameters.P
InvoiceQuery1.Parameters.P
InvoiceQuery1.Open;
The above satisfies the Delphi compiler and the application now executes without a problem
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"