Link to home
Start Free TrialLog in
Avatar of joshfly
joshfly

asked on

HOW TO RETRIEVE THE TIME USING SQL STATEMENT

hello there,
can i retrieve only the 'time' from the datetime?

exmaple:
A datetime like '2004-09-02 13:42:002', i want to take out only the time '13:42:002'

is it possible to do this?
and some more i want to find out the difference between two date/datetime.
It can be done using DATEDIFF in SQL but it only give me the result in either hour or minute or second or m. Second.

I want to result to be something like this :-
The Difference between this two date is 1 hour 30 minutes 39 second OR 1:30:39

thanks you
josh
Avatar of kretzschmar
kretzschmar
Flag of Germany image

1.TO_Char(DateTimeField,'HH:MI:SS');

2. Difference := DateTimeField1 - DateTimeField2;
whereas the frac part of the result is the time,
and the part before the decimal-delimiter are days

meikl ;-)
to get the time from a datetime you can use decodetime from sysutils

procedure TForm1.Button1Click(Sender: TObject);
var
 Year, Month, Day, Hour, Min, Sec, MSec: Word;
 begin
  DecodeTime(Now, Hour, Min, Sec, MSec);
end;
oops, sorry, i thought i was in the oracle topic

1. result := TimeToStr(DateField);

2. result := timetostr(DateField1-Datefield2);

meikl ;-)
mmm...
hi meikl, nice to see you :))

for 2 i'd do
 result := TimeToStr((SecondsBetween(datefield1,datefield2))/(60*60*24));
Avatar of Voodooman
Voodooman

Hi

The sql depends what database you are using. What db is it?


Voodooman
good point, voodoo,

overlooked "USING SQL STATEMENT" myself
In Delphi you can do simple math with the TDateTime values. The trunc(Now) part is the date-part while the Frac(Now) part is the time part. Something like:

Time3 := Abs(Frac(DateTime1) - Frac(DateTime2));

Then Time3 will be the difference between both times. Using FormatDateTime('HH:NN:SS', Time3) would then provide you the difference in hours, minutes and seconds.

But those are just the Delphi solutions. Solving this in SQL is a bit harder since it depends on the SQL dialect that you're using. SQL is a bit limited in the additional functions it provides. But if I'm not mistaken, many SQL databases do store date/time fields in the same way as Delphi: as floats...

But you might want a refund and re-ask this question in one of the Database-specific areas of EE. It has no relation to Delphi anyway.
ASKER CERTIFIED SOLUTION
Avatar of BlackTigerX
BlackTigerX

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
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
Avatar of joshfly

ASKER

thanks guys,

:)
josh