Solved

MySQL - Date Query

Posted on 2004-08-09
8
539 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

16 Experts available now in Live!

Get 1:1 Help Now