• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2270
  • Last Modified:

DateTime troubles

Hello,

I'm trying to post a date (yyyymmdd) into a SQL table that uses a date type field.  Every time I do so with Query Analyzer in the same format, it works fine, but in Delphi using...

FieldByName('Date').asstring := '20050305'

I receive 20050305 is not a valid date.

So then I figured I would try to use

FieldByName('Date').asdatetime := strtodate('20050305')

And that fails with the same error.  I also tried to set ShortDateFormat to = 'yyyymmdd' but with no luck.

Please help.
0
looknow12
Asked:
looknow12
  • 3
  • 2
  • 2
  • +7
1 Solution
 
aikimarkCommented:
Try this format with your string:
yyyy-mm-dd
0
 
ralph44eCommented:
depending what date format you're server is set too

FieldByName('Date').value:=03/25/2005;
or
FieldByName('Date').value:=2005-03-25;
0
 
mikelittlewoodCommented:
Best to find out what your server is set to.
You could have one of many more date formats listed above, so why not go to the database and find a table with a date column to check the format
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
esoftbgCommented:
var
  DT:    TDateTime;
  SD:    string;
begin
  SD := '20050305'; // or SD := '2005/03/05'; or ....... use what does not raise an exception on the next line
  DT := StrToDate(SD);
  FieldByName('Date').Value := TDateTime(DT);
end;
0
 
RYasinCommented:
hi .. i think u have to use formatdatetime()
like this :

FieldByName.('Date').Value := FormatDateTime('yyyy/mm/dd',Date*)

*Date: will get the date of system so u can give an value to the field.

0
 
_Katka_Commented:
Hi,

you have to set MSSQL database to accept
your format, this can be done by sending
as a batch script:

SET DATEFORMAT ymd

(other formats: mdy,dmy,ydm,myd and dym)
(by default: mdy)

then set Delphi global variables like this:

ShortDateFormat:='yyyymmdd';
DateSeparator:='/';

regards,
Kate


0
 
Lee_NoverCommented:
doesn't anyone use parameters ?

qy.SQL.Text:='UPDATE Table SET Date = :Date WHERE ID = :ID';
qy.ParamByName('Date').AsDateTime:=SomeDate; // or EncodeDate(2005, 3, 5) or whatever :)
qy.ParamByName('ID').AsInteger:=SomeID;

works regardles of the system date format because the provider handles that
0
 
mikelittlewoodCommented:
I guess not as much Lee  :o)

I know I don't
0
 
looknow12Author Commented:
I believe trying to convert the date should be a last resort.  Remember I am stuck with the format yyyymmdd and I don't want to start adding slashes or dashes to make up what the strtodate is expecting.  

I prefer not to get into changing the date itself.    In fact (perhaps I gave too much information) the real problem is why won't the string date get forced into the date field in SQL.    I can do the same thing using an update statement?

As you can see however, I am treating the SQL table as a dataset because there's a lot of fields and I feel the code looks cleaner and easier to create than to have to create a messy update command.

Here's a code snippet (at the time):

               with quShipment do
                    begin
                      active := False;
                      sql.Text := 'select * from SHIPMENT_TRACKING_DETAILS where TRACKING_NUMBER = ''' + dxmemdata1.FieldByName('Tracking Number').AsString + '''';
                      active := True;


                      if RecordCount > 0 then  //tracking number already exists, what is this!  Fail the record and record in log
                        begin
                          MemLog.FieldByName('TrackingAlreadyExists').AsString :=  'True';
                          memlog.Post;
                        end
                      else // tracking number is a virgin, store this tracking number to that document
                        begin
                          append;
                          FieldbyName('DOC_NO').AsString          :=    quPo.FieldByName('Ref_no').AsString;
                          DateSeparator := '';
                          shortdateformat := 'yyyymmdd';
 ----------->>>    FieldByName('DELIVERY_DATE').asdatetime   :=    strtodate('20050305');
                          Post;

------------much deleted-----------------


0
 
looknow12Author Commented:
Dateseparator := ''; will not compile btw
0
 
aikimarkCommented:
looknow12,

<<FieldByName('DELIVERY_DATE').asdatetime   :=    strtodate('20050305');>>
In order to use the strtodate() function, the string needs to be an acceptable date format.  Rreferencing http://www.delphibasics.co.uk/RTL.asp?Name=StrToDate&ExpandCode1=Yes
"The date string must adhere to the format of the ShortDateFormat value, and use the DateSeparator character to separate the day, month and year values. "

================================
<<FieldByName('Date').asstring := '20050305'>>
From this comment in your original question, I stated you needed to use the format yyyy-mm-dd.  This should be an acceptable date to SQL Server.  Did you try this?

================================
<<I believe trying to convert the date should be a last resort.  Remember I am stuck with the format yyyymmdd ...>>
* You currently have the yyyymmdd formatted string.  You are NOT 'stuck' with this.  This is merely a starting point.
* From a review of the experts' comments in this thread, I would say we all disagree with you.  This is the solution.  Please pick one and we will help you implement it.  If you do not know how to reformat this string, please ask and we will be glad to help.
0
 
Lee_NoverCommented:
I'd rewrite that to:

with quShipment do
begin
  if Active then
    Close;
  sql.Text := 'select * from SHIPMENT_TRACKING_DETAILS where TRACKING_NUMBER = :TN';
  ParamByName('TN').AsString:=dxmemdata1.FieldByName('Tracking Number').AsString;  // if the Tracking number is really an integer then use AsInteger
  Open;

  // use (Eof and Bof) to determine that the resultset is empty - RecordCount executes the query again !!!
  if not (Eof and Bof) then  //tracking number already exists, what is this!  Fail the record and record in log
  begin
    MemLog.FieldByName('TrackingAlreadyExists').AsString :=  'True';
    memlog.Post;
  end
  else // tracking number is a virgin, store this tracking number to that document
  begin
    Append;
    FieldbyName('DOC_NO').AsString          :=    quPo.FieldByName('Ref_no').AsString;
    FieldByName('DELIVERY_DATE').AsDateTime   :=    EncodeDate(2005, 3, 5);
    Post;

where do you get that date from anyway ?

and update statements are not messy unless you make them messy
in your case why do you retrieve the whole row if you just wanna know if there's a record that fits that criteria ?
instead you should only do

with quShipment do
begin
  if Active then
    Close;
  sql.Text := 'select Count(*) as RecCount from SHIPMENT_TRACKING_DETAILS where TRACKING_NUMBER = :TN';
  ParamByName('TN').AsString:=dxmemdata1.FieldByName('Tracking Number').AsString;  // if the Tracking number is really an integer then use AsInteger
  Open;
  if FieldByName('RecCount').AsInteger > 0 then ...


0
 
delphinewbieCommented:
try

onformCreate:

Application.UpdateFormatSettings := False;
ShortDateFormat := 'yyyyMMdd';


I recall reading somewhere that windows overrides the shordate assignation by default.
The first line of the code above stops this from happening.

May work, may not - but its not an overly complex solution to try :)

0
 
TheRealLokiSenior DeveloperCommented:
just ignore all the horrible date time formats the system and/or the sql server, and simply do :-
s := '20050305';  // yyyymmdd
FieldByName('DELIVERY_DATE').asdatetime := EncodeDate( StrToInt(copy(s, 1, 4)), StrToInt(copy(s, 5, 2)), StrToInt(copy(s, 7, 2)) );
0
 
looknow12Author Commented:
Accepted Answer from TheRealLoki
Date: 03/28/2005 06:52PM PST
Grade: A
 Accepted Answer  


just ignore all the horrible date time formats the system and/or the sql server, and simply do :-
s := '20050305';  // yyyymmdd
FieldByName('DELIVERY_DATE').asdatetime := EncodeDate( StrToInt(copy(s, 1, 4)), StrToInt(copy(s, 5, 2)), StrToInt(copy(s, 7, 2)) );


Thanks, that was the easiest solution to implement.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now