Solved

HOW TO RETRIEVE THE TIME USING SQL STATEMENT

Posted on 2004-09-02
10
364 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:joshfly
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11961932
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 ;-)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 11961947
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;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11961956
oops, sorry, i thought i was in the oracle topic

1. result := TimeToStr(DateField);

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

meikl ;-)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 11962008
mmm...
hi meikl, nice to see you :))

for 2 i'd do
 result := TimeToStr((SecondsBetween(datefield1,datefield2))/(60*60*24));
0
 
LVL 5

Expert Comment

by:Voodooman
ID: 11962617
Hi

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


Voodooman
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 11962632
good point, voodoo,

overlooked "USING SQL STATEMENT" myself
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11963852
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.
0
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 50 total points
ID: 11964851
you could use this:

select convert(varchar, getdate(), 8)

that will give you the time of the current date, of course you can replace getdate with any datetime field you have, and you can assign it to any variable, etc

declare @myvar datetime
set @myvar = convert(varchar, getdate(), 8) --extract the time
select convert(varchar, @myvar, 8) --show the time
0
 
LVL 6

Assisted Solution

by:bpana
bpana earned 30 total points
ID: 11966318
i assume you use SQL Server

declare @date1 datetime, @date2 datetime
declare @hh int, @mm int, @ss int
declare @datediffs int
set @date1 = '2004-09-02 13:42:002'
set @date2 = '2004-09-02 14:43:003'

set @datediffs = datediff(ss, @date1, @date2)
set @hh = round(@datediffs/3600, 0)
set @datediffs = @datediffs - @hh*3600
set @mm = round(@datediffs/60, 0)
set @datediffs = @datediffs - @mm*60
select cast(@hh as varchar(2)) + ':' + cast(@mm as varchar(2)) + ':' + cast(@datediffs as varchar(2))

the result is not nice, but you got the ideea, you can format it as you want
0
 

Author Comment

by:joshfly
ID: 11993155
thanks guys,

:)
josh
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 89
Dev Express grid collapse 2 39
Reconfigure Delphi Install? 2 45
drawing animated level bar based on numbers 3 86
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now