Retrieving SQL Records between dates with Delphi ADO

Posted on 2014-08-20
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 47

    Expert Comment

    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

    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 47

    Expert Comment

    :) 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
    LVL 44

    Expert Comment

    by:Vitor Montalvão
    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 25

    Accepted Solution

    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

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now