Solved

MySQL - Date Query

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 73
tidtcpserver connection lost handle 2 83
PHP preg_replace code convert to Delphi 14 55
Firemonkey allowing RTL on android 6 29
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

785 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