Solved

HOW TO RETRIEVE THE TIME USING SQL STATEMENT

Posted on 2004-09-02
10
363 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

7 Experts available now in Live!

Get 1:1 Help Now