Avatar of firekiller15
firekiller15
 asked on

how to convert timestamp to datetime

delphi

can i know what is the major differences of timestamp and datetime

in my MSSQL database i have data type timestamp

when i try to compile my delphi application error occur

i call record by doing this

fieldbyname('A').asdatetime;
is it wrong to use datetime here??
Editors IDEsDelphi

Avatar of undefined
Last Comment
Lukasz Zielinski

8/22/2022 - Mon
kretzschmar

in case if using delphi 7 -> there was a bug, fixed with the first update

see

http://dn.codegear.com/article/32337

meikl ;-)
firekiller15

ASKER
.....
my question is
if my dataype for my attribute is timestamp in SQL server
in my delphi can i use this code
fieldbyname('A').asdatetime;
is it wrong to use asdatetime here??
Lukasz Zielinski

why don't you use datetime type in SQLServer?

ziolko.
Your help has saved me hundreds of hours of internet surfing.
fblack61
kretzschmar

.asDateTime is the correct

what error do you get?
firekiller15

ASKER
why don't you use datetime type in SQLServer?
is there any different btw datetime and timestamp
firekiller15

ASKER

.asDateTime is the correct

what error do you get?

=> cannot access field 'a' as type DateTime
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Lukasz Zielinski

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kretzschmar

but at least it should be readable . . .
Lukasz Zielinski

I'm sure it's readable but it's format might not be recognizable when call .AsDateTime

ziolko.
kretzschmar

ziolko . . . read the link i gave above
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Lukasz Zielinski

I didi read it, there are some fixes regarding timestamp but the question remains if asker needs timestamp or datetime field, at least on MSSQL it does matter.
And even more important, what is the error when he is using .AsDateTime() if we don't know error how can we say if this update will fix it

ziolko.
SteveBay

MSSQL TimeStamp is not (to my knowledge) useful in Delphi as a DateTime. Someone please correct me if I am wrong.
It is useful as indicator of update by another user. It needs to be read as a variant then converted to a string for comparison in a where clause.
You would use this to avoid overwriting someone else's changes.
Simple sample application attached.

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, SqlTimSt;
 
type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    btnReadRecord: TButton;
    btnWriteRecord: TButton;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    procedure btnReadRecordClick(Sender: TObject);
    procedure btnWriteRecordClick(Sender: TObject);
  private
     AcctID : Integer;
     TimeSt :  String;
     AcctName : String;
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
Type
  SQLTimestamp = array [0..7] of Byte;
  PSQLTimestamp = ^SQLTimestamp;
 
implementation
 
{$R *.dfm}
 
function TimeStampToStr(const vTS: Variant): string;
var p: PSQLTimestamp;
begin
	p := VarArrayLock(vTS);
	try
		Result := Format('0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x', [p^[0], p^[1], p^[2], p^[3], p^[4], p^[5], p^[6], p^[7]]);
	finally
		VarArrayUnlock(vTS);
	end;
end;
 
 
procedure TForm1.btnReadRecordClick(Sender: TObject);
begin
     ADOQuery1.Close;
     ADOQuery1.SQL.Clear;
     ADOQuery1.SQL.Add('SELECT ID, Name, timestamp_column FROM Resource' );
     ADOQuery1.SQL.Add(' WHERE Name = ''Chicago''');
     ADOQuery1.Active :=  TRUE;
 
     AcctID := ADOQuery1.FieldByName('ID').AsInteger;
     AcctName := ADOQuery1.FieldByName('Name').AsString;
     TimeSt := TimeStampToStr(ADOQuery1.FieldByName('timestamp_column').AsVariant);
end;
 
procedure TForm1.btnWriteRecordClick(Sender: TObject);
begin
     ADOQuery1.Close;
     ADOQuery1.SQL.Clear;
     ADOQuery1.SQL.Add('UPDATE [Resource] SET [Name] = ' + QuotedStr(AcctName));
     ADOQuery1.SQL.Add(' WHERE ID = ' + IntToStr(AcctID) );
     ADOQuery1.SQL.Add(' AND timestamp_column = ' + TimeSt );
     ADOQuery1.ExecSQL;
end;
 
end.

Open in new window

ASKER CERTIFIED SOLUTION
SteveBay

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
kretzschmar

puh, never guessed that . . . learned again from you, SteveBay
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Lukasz Zielinski

hmm I guess i wrote about differences between datetime and timestamp:
http://Q_23314229.html#21331964 
altough without details:)

ziolko.