473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How calculate date/time difference in DB2

I found this example in MYSQL:
create table events
( id integer not null primary key
, datetime_start datetime not null
, datetime_end datetime not null
);
insert into events values
( 1, '2006-09-09 14:00', '2006-09-09 16:00' )
,( 2, '2006-09-10 09:00', '2006-09-10 17:00' )
,( 3, '2006-09-11 13:30', '2006-09-11 14:45' )
,( 4, '2006-09-12 09:00', '2006-09-15 17:00' );

select id
, datetime_start
, datetime_end
, datetime_end - datetime_start as diff
from events
id datetime_start datetime_end diff
1 2006-09-09 14:00 2006-09-09 16:00 20000
2 2006-09-10 09:00 2006-09-10 17:00 80000
3 2006-09-11 13:30 2006-09-11 14:45 11500
4 2006-09-12 09:00 2006-09-15 17:00 3080000

Can we get in DB2 date/time difference in one query?
Or we have calculate it separate?
Thank's in advance Leny G.

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200809/1

Sep 2 '08 #1
4 50096
"lenygold via DBMonster.com" <u41482@uwewrote in message
news:8997776c4097f@uwe...
>I found this example in MYSQL:
create table events
( id integer not null primary key
, datetime_start datetime not null
, datetime_end datetime not null
);
insert into events values
( 1, '2006-09-09 14:00', '2006-09-09 16:00' )
,( 2, '2006-09-10 09:00', '2006-09-10 17:00' )
,( 3, '2006-09-11 13:30', '2006-09-11 14:45' )
,( 4, '2006-09-12 09:00', '2006-09-15 17:00' );

select id
, datetime_start
, datetime_end
, datetime_end - datetime_start as diff
from events
id datetime_start datetime_end diff
1 2006-09-09 14:00 2006-09-09 16:00 20000
2 2006-09-10 09:00 2006-09-10 17:00 80000
3 2006-09-11 13:30 2006-09-11 14:45 11500
4 2006-09-12 09:00 2006-09-15 17:00 3080000

Can we get in DB2 date/time difference in one query?
Or we have calculate it separate?
Thank's in advance Leny G.
Try the SQL Reference manual Vol 1
http://www-01.ibm.com/support/docvie...id=swg27009727
Sep 2 '08 #2
you can use TIMESTAMPDIFF function.
But, I think that it is not accurate, it is estimation.

Please see here for basic handlings of date/time data types on DB2.
Fun with Dates and Times
http://www.ibm.com/developerworks/db.../0211yip3.html

You may be able to use the folowing expression for exsact difference
of timestamp.
DAYS(datetime_end )*24*60*60 + MICROSECONDS(datetime_end )
-
DAYS(datetime_start)*24*60*60 + MICROSECONDS(datetime_start)
Sep 2 '08 #3
I saw the following descriptions in the article.

Using timestampdiff() is more accurate when the dates are close
together than when they are far apart. If you need a more precise
calculation, you can use the following to determine the difference in
time (in seconds):

(DAYS(t1) - DAYS(t2)) * 86400 +
(MIDNIGHT_SECONDS(t1) - MIDNIGHT_SECONDS(t2))

For convenience, you can also create an SQL user-defined function of
the above:

CREATE FUNCTION secondsdiff(t1 TIMESTAMP, t2 TIMESTAMP)
RETURNS INT
RETURN (
(DAYS(t1) - DAYS(t2)) * 86400 +
(MIDNIGHT_SECONDS(t1) - MIDNIGHT_SECONDS(t2))
)
@
Sep 2 '08 #4
You can use TIMESTAMPDIFF function.
But, I think that it is not accurate, it is estimation.

Please see here for basic handlings of date/time data types on DB2.
Fun with Dates and Times
http://www.ibm.com/developerworks/db...le/0211yip/021...

You may be able to use the folowing expression for exsact difference
of timestamp.
DAYS(datetime_end )*24*60*60 + MIDNIGHT_SECONDS(datetime_end )
-
DAYS(datetime_start)*24*60*60 + MIDNIGHT_SECONDS(datetime_start)
Also, I saw the follwing descriptions in the article "Fun with Dates
and Times".

Using timestampdiff() is more accurate when the dates are close
together than when they are far apart. If you need a more precise
calculation, you can use the following to determine the difference in
time (in seconds):

(DAYS(t1) - DAYS(t2)) * 86400 +
(MIDNIGHT_SECONDS(t1) - MIDNIGHT_SECONDS(t2))

For convenience, you can also create an SQL user-defined function of
the above:

CREATE FUNCTION secondsdiff(t1 TIMESTAMP, t2 TIMESTAMP)
RETURNS INT
RETURN (
(DAYS(t1) - DAYS(t2)) * 86400 +
(MIDNIGHT_SECONDS(t1) - MIDNIGHT_SECONDS(t2))
)
@
Sep 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter | last post by:
Hi, I want to calculate the time difference (in seconds, minutes, days, etc.) between to DateTime values. I expected some of the DateTime methods could do the job for me, but after some...
4
by: Vivek Sharma | last post by:
Hi, I need to calculate the time difference between today 2pm and tomorrow 2 am. How do I calculate? Thanks Vivek
2
by: Lad | last post by:
I use datetime class in my program and now I have two fields that have the datetime format like this datetime.datetime(2006, 5, 24, 16, 1, 26) How can I find out the date/time difference ( in...
7
by: walt | last post by:
Hello, I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to...
3
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
3
by: Steve | last post by:
Hello all, I was wondering if someone could help me here. Since I'm running and iMac, I don't have Visual Basic (6) installed. I want a small function returns the number of seconds between two...
3
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
2
by: Guruganesh | last post by:
i have to calculate the time diference of two file names namely File 1: CBE03KMD_2010_03_08_23_06_36.upl File 2: CBE03KMD_2010_03_08_22_11_47.upl see the date is same but the time is also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.