Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using variable in SQL-statement

0 views
Skip to first unread message

Aimo Aronen

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

Hi all !

I'm new here and I don't know if this has been asked earlier, but I've
strugled almost whole day trying to use variable (including date-data)
SQL-stament.

Fos example, if I use:
'SELECT * FROM MyTable WHERE MyDateField <= ''11/19/1996'''
it works fine and shows all the records that are older or equal to above
date.

How can I put a variable in the place where I have the "11/19/1996"

Regards,

jefsu...@aol.com

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

In article <32A35B...@netti.fi>, Aimo Aronen <aim...@netti.fi>
writes:

Two main methods. First, use a parameter. I don't use those that often,
but the idea is in the SQL use:
Select * from MyTable where MyDateField <= :MyDate
then set the params property of the query to your value.

A more flexible method is to modify the SQL property of the query in code.
For example, in response to a buttock click (or form activate or form
show):
with Query1 do begin
SQL.clear;
SQL.add('select * from MyTable where MyDateField <= "' +
edit1.text+'"');
active := True;
end;
This takes the text of the Edit box (Edit1.text) and puts it into the
query, then activates the query. This method allows a great deal of
flexibility in creating complex queries.

günther

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

Hi Aimo!

> Fos example, if I use:
> 'SELECT * FROM MyTable WHERE MyDateField <= ''11/19/1996'''

> How can I put a variable in the place where I have the "11/19/1996"

try:

'SELECT * FROM MyTable WHERE MyDateField <= :anydate'

Qry.ParamByName('anydate').AsString := '01/01/97';

then run the query...

or

Qry.ParamByName('anydate').AsDateTime := now;

reguards guenther


Benoit Courchesne

unread,
Dec 2, 1996, 3:00:00 AM12/2/96
to

Aimo Aronen <aim...@netti.fi> wrote in article <32A35B...@netti.fi>...
> Hi all !


>
> I'm new here and I don't know if this has been asked earlier, but I've
> strugled almost whole day trying to use variable (including date-data)
> SQL-stament.
>

> Fos example, if I use:
> 'SELECT * FROM MyTable WHERE MyDateField <= ''11/19/1996'''

> it works fine and shows all the records that are older or equal to above
> date.
>

> How can I put a variable in the place where I have the "11/19/1996"
>

> Regards,
>

You had to worked with params ex :

your SQL statements should look like that

SELECT * FROM MyTable
WHERE MyDateField =: DateVar

And in the properties Params of your SQL
you should have a DateVar Declared


After that the SQL will be call by :

SQL.close;
Q.ParamByName('DateVar').AsDateTime := StrToDateTime (YourDate);
Q.Open;

Benoit Courchesne
Universite du Quebec a Montreal
Departement d'informatique
df19...@er.uqam.ca


0 new messages