Date formats

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Willo
Posts: 34
Joined: Thu 24 Aug 2006 18:29

Date formats

Post by Willo » Wed 12 Sep 2007 23:19

hi;

i noticed what for me is a strange behavior, but i dont know if this come from mysql, delphi or mydac.

i have this sentence:

Select re_fven,(case when SUBSTRING(RE_NUM,1,1) = '1' then DATE_ADD(RE_FVEN,INTERVAL 15 DAY) else DATE_ADD(RE_FVEN,INTERVAL -15 DAY) end) as fechavencido from recibos

and after that, i want to append those dates into a VirtualTable, again on 2 different datetime fields, like this

Code: Select all

VirtualT.Append;
vdate1.asdatetime := Q1.Fieldbyname('re_fven').asdatetime;
vdate2.asdatetime := Q1.Fieldbyname('fechavencido').asdatetime;
VirtualT.Post;
but it didnt work like that... i got "invalid date" errors, so i changed the code like this:
VirtualT.Append;
xfecha := Qcartas.FieldByname('re_fven').Asstring;
vdate1.asdatetime := EncodeDate(StrToInt(copy(xfecha,7,4)), StrToInt(copy(xfecha,4,2)),
StrToInt(copy(xfecha,1,2)));

xfecha := Qcartas.FieldByname('fechavencido').Asstring;
vdate2.asdatetime := EncodeDate(StrToInt(copy(xfecha,1,4)),
StrToInt(copy(xfecha,6,2)),
StrToInt(copy(xfecha,9,2)));
VirtualT.Post;

Please note that original table field (RE_FVEN) is returned with different date format that calculated field (fechavencido).

why is that?

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 13 Sep 2007 13:36

The CASE operator converts DATETIME values to strings, and applies the DATETIME format of MySQL. To solve this problem you can either change the datetime_format server parameter, or perform an explisit cast to DATETIME:

Code: Select all

  cast((case when SUBSTRING(RE_NUM,1,1) = '1'   
             then DATE_ADD(RE_FVEN,INTERVAL 15 DAY)   
             else DATE_ADD(RE_FVEN,INTERVAL -15 DAY) end) 
       as datetime) as fechavencido1

Post Reply