Solved

MySQL - Date Query

Posted on 2004-08-09
8
547 Views
Last Modified: 2010-05-18
How to pass a SQL statement in MySQL query database with date as parameter.

I am using Delphi7 and TMySQL ( MySQL Direct Access Component).
0
Comment
Question by:smartvanl5
8 Comments
 
LVL 6

Accepted Solution

by:
rbohac earned 50 total points
ID: 11759075
I'm not sure about the component you are using by I always do this..


var MyDate:String;
begin
MyDate := '2004-08-09';
SQL := 'select * from database where mydate = '+QuotedStr(MyDate);
//execute query
end;

You can also get the date from a TDateTime type by: MyDate := FormatDateTime('yyyy-mm-dd',now);
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11760634
Use a parameterised query...

SQL := 'select * from Whatever where MyDate = :MyDate';

Then assign the parameter to the query by using the Parameters property of your Query component. Exactly how is based upon how you're using the Query component. If you assign the query in design-time, you can also create the parameters in designtime. If you assign the query in runtime, you must fill the parameters property too in runtime, after assigning the query...
0
 

Expert Comment

by:krypto2000
ID: 11819167
Hello,

The mySQL date format is YYYY-MM-DD

so i've making a little function for convert TDate to YYYY-MM-DD

====================================================
function dateToSQLDate(Date: TDate): string;
var Day, Month, Year: word;
begin
   DecodeDate(Date, Year, Month, Day);
   result := IntToStr(Year) + '-' + IntToStr(Month) + '-' + IntToStr(Day);
end;
====================================================

after you can execute your query like this :

myQuery.close;
myQuery.paramByName('myDate').asString := dateToSQLDate(Now);
myQuery.open{ or execSQL };


I hope that will help you ;-)
0
 
LVL 6

Expert Comment

by:rbohac
ID: 11819647
You can also simply call

  FormatDateTime('yyyy-mm-dd hh:nn:ss',Now)
0
 
LVL 1

Expert Comment

by:Balshe
ID: 11951271

Var
qry:Tquery;
...
...
...

qry.sql.add('Select * from Table where MyDate=:MyDate');
qry.parameterbyname('Mydate').asdatetime:=TheDate; // you can use "NOW"
qry.open;

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

680 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