0

I'm having trouble adding a date field into my SQL statement - no syntax error but it saves the date as 1905-06-17 any tips please?

The var is

ExpireDate : String;

Value

ExpireDate := DateToStr(IncYear(Today,1));

Statement

datamodule1.qryDB1.SQL.Text := 'INSERT INTO table SET Member_ExpireDate = '+ ExpireDate +' WHERE Member_ID = .................';
2
  • You need to insert DateTime ? Commented Sep 16, 2016 at 16:19
  • How hard did you try looking before asking this q? Google found this easily enough: stackoverflow.com/questions/27271448/… Commented Sep 16, 2016 at 16:22

3 Answers 3

1

You are using the version of DateToStr() that relies on the user's current OS locale settings. DBs have particular formats that they expect date/time strings to be expressed in, and the OS's locale settings might not match those formats. You can use the overloaded version of DateToStr() that takes a TFormatSettings as input so you can specify the exact formatting of the output string (via the TFormatSettings.ShortDateFormat member):

var
  fmt: TFormatSettings;
  ExpireDate: string;

fmt := TFormatSettings.Create;
fmt.ShortDateFormat := 'yyyy-mm-dd';
ExpireDate := DateToStr(IncYear(Today,1), fmt);

Also, date/time strings need to be wrapped in quotes in SQL statements. You can use the QuotedStr() function for that purpose.

But more importantly, your INSERT statement is malformed anyway.

If you are trying to add a new record, it needs to look more like this instead:

datamodule1.qryDB1.SQL.Text := 'INSERT INTO table (Member_ID, Member_ExpireDate) VALUES (' + SomeIDValue + ', ' + QuotedStr(ExpireDate) + ')';

Otherwise, if you are trying to modify an existing record, you need to use an UPDATE statement instead:

datamodule1.qryDB1.SQL.Text := 'UPDATE table SET Member_ExpireDate = ' + QuotedStr(ExpireDate) + ' WHERE Member_ID = ' + SomeIDValue;

Either way, it would be best to use a parameterized query instead. Let the DB itself handle all of the necessary formatting for you:

datamodule1.qryDB1.SQL.Text := 'INSERT INTO table (Member_ID, Member_ExpireDate) VALUES (:MemberID, :ExpireDate)';
datamodule1.qryDB1.ParamByName('MemberID').AsInteger := SomeIDValue; // or whatever data type your field uses
datamodule1.qryDB1.ParamByName('ExpireDate').AsDate := IncYear(Today,1);
datamodule1.qryDB1.ExecSQL;

datamodule1.qryDB1.SQL.Text := 'UPDATE table SET Member_ExpireDate = :ExpireDate WHERE Member_ID = :MemberID';
datamodule1.qryDB1.ParamByName('ExpireDate').AsDate := IncYear(Today,1);
datamodule1.qryDB1.ParamByName('MemberID').AsInteger := SomeIDValue; // or whatever data type your field uses
datamodule1.qryDB1.ExecSQL;
Sign up to request clarification or add additional context in comments.

Comments

1

Don't convert the date to string. Insert the data as date and use parametrs to minimize the errors. Using parameters the component format the datatime as correct format.

var
  ExpireDate : TDate;
begin
  ExpireDate := IncYear(Today,1);
  datamodule1.qryDB1.SQL.Text := 
     'INSERT INTO table SET Member_ExpireDate = :expireDate WHERE Member_ID = ..';
  datamodule1.qryDB1.Params.ParamByName('expireDate').Value := ExpireDate;

Depending on the components you use, ParamByName may be some different, but all TQuery descendant components have this method.

Regards.

Comments

0

Thanks for the response problem solved... should have used #'+ExpireDate+'# all working great now thanks.

1 Comment

This is not entirely accurate. you need a specific date format besides the # prefix/suffix. i.e '#' + FormatDateTime('yyyy-MM-dd', DT) + '#' read @Remy's answer for details. Your soltution may fail on other OS using different locale.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.