Link to home
Start Free TrialLog in
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??
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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

see

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

meikl ;-)
Avatar of firekiller15
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??
why don't you use datetime type in SQLServer?

ziolko.
.asDateTime is the correct

what error do you get?
why don't you use datetime type in SQLServer?
is there any different btw datetime and timestamp

.asDateTime is the correct

what error do you get?

=> cannot access field 'a' as type DateTime
SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but at least it should be readable . . .
I'm sure it's readable but it's format might not be recognizable when call .AsDateTime

ziolko.
ziolko . . . read the link i gave above
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.
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
puh, never guessed that . . . learned again from you, SteveBay
hmm I guess i wrote about differences between datetime and timestamp:
http://Q_23314229.html#21331964 
altough without details:)

ziolko.