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
Solved

MySQL - Date Query

Posted on 2004-08-09
8
543 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

792 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