TMyQuery alters format of date strings?
Posted: Mon 19 Mar 2018 15:13
I'm having a problem with the format of strings returned by TMyQuery for date fields in MySQL.
I created a test table using
Running the following SQL in my DBMS (SQLyog)
produces, as expected
However using a TMyconnection and a TMyQuery (both with default properties)in Delphi, the same query produces
ie the field date_date is shown in a different format, which really mucks up further processing in the application. For example where the dates are put into a dropdown box for later selection and use in a WHERE clause., as that produces sql of the form
which produces no results.
This can be shown by the delphi code
that will show '12/03/2018' is being held in the variable TheDate.
I realise that I can get the result I want by using CAST or CONVERT eg
but where is the setting that is telling TMyQuery to convert the date to the format dd/mm/yyyy instead of leaving it in the yyyy-mm-dd format that was used when entering it ? (And why does my DBMS show a different, but as far as I am concerned a correct, result?)
I created a test table using
Code: Select all
CREATE TABLE testtable ( letter CHAR(1) , date_date DATE, date_str VARCHAR(10) );
INSERT INTO testtable (letter, date_date, date_str) VALUES ('A', '2018-03-12', '2018-03-12');
Code: Select all
SELECT letter, date_date, date_str FROM testtable;
Code: Select all
letter | date_date | date_str
'A' | '2018-03-12' | '2018-03-12'
Code: Select all
letter | date_date | date_str
'A' | '12/03/2018' | '2018-03-12'
Code: Select all
SELECT * FROM testtable WHERE date_date = '12/03/2018';
This can be shown by the delphi code
Code: Select all
var TheDate : string;
begin
TMyQuery1.SQL.Clear;
TMyQuery1.SQL.Add('SELECT date_date FROM testtable;');
TMyQuery1.open ;
TheDate := TMyQuery1.FieldByName('date_date').AsString ;
ShowMessage(theDate);
end;
I realise that I can get the result I want by using CAST or CONVERT eg
Code: Select all
SELECT CONVERT(date_date, char) FROM testtable;