Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL - Date Query

Posted on 2004-08-09
8
Medium Priority
?
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Accepted Solution

by:
rbohac earned 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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