I had two TIME fields that i wanted to know the difference in between
them. I saw TIMESTAMPDIFF but nothing equivalent for TIME. Further, i
want HH:MM:SS format. So, i wrote this FUNCTION.
Is there a better way to do it?
CREATE FUNCTION TimeDiff(Time_1 TIME, Time_2 TIME)
RETURNS TIME
SPECIFIC TimeDiff
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
BEGIN ATOMIC
DECLARE Diff CHAR(22);
SET Diff = CHAR(TIMESTAMP( '1/1/0001', Time_1) - TIMESTAMP('1/1/0001',
Time_2));
RETURN TIME
(
CHAR
(
RTRIM(CHAR(TIME STAMPDIFF(8, Diff)))
|| ':' || RIGHT(RTRIM('0' || CHAR(Mod(TIMEST AMPDIFF(4, Diff), 60))),
2)
|| ':' || RIGHT(RTRIM('0' || CHAR(Mod(TIMEST AMPDIFF(2, Diff), 60))),
2)
)
);
END
B. 7 9144
Brian Tkatch wrote: I had two TIME fields that i wanted to know the difference in between them. I saw TIMESTAMPDIFF but nothing equivalent for TIME. Further, i want HH:MM:SS format. So, i wrote this FUNCTION.
Is there a better way to do it?
You can subtract two TIME columns? Result is a DECIMAL(6,0) = HHMMSS
(similar to TIMESTAMPDIFF return value).
$ db2 "values (current time - time('12:05:10' ))"
1
--------
73912.
1 record(s) selected.
73912 = 7H 39M 12S
So, you could write the function as:
CREATE FUNCTION TimeDiff(Time_1 TIME, Time_2 TIME)
RETURNS CHAR(8)
SPECIFIC TimeDiff
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
case when Time_1 >= Time_2 then
substr(digits(T ime_1 - Time_2),1,2) || ':' ||
substr(digits(T ime_1 - Time_2),3,2) || ':' ||
substr(digits(T ime_1 - Time_2),5,2)
else
raise_error('70 001','Time_1 must be greater than Time_2')
end;
Note CHAR(8) as return data type (Duration <> time). If you don't
agree, it's trivial to convert this to TIME.
Good luck,
Brian Tkatch wrote: CREATE FUNCTION TimeDiff(Time_1 TIME, Time_2 TIME) RETURNS TIME SPECIFIC TimeDiff DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL BEGIN ATOMIC
DECLARE Diff CHAR(22);
SET Diff = CHAR(TIMESTAMP( '1/1/0001', Time_1) - TIMESTAMP('1/1/0001', Time_2));
RETURN TIME ( CHAR ( RTRIM(CHAR(TIME STAMPDIFF(8, Diff))) || ':' || RIGHT(RTRIM('0' || CHAR(Mod(TIMEST AMPDIFF(4, Diff), 60))), 2) || ':' || RIGHT(RTRIM('0' || CHAR(Mod(TIMEST AMPDIFF(2, Diff), 60))), 2) ) );
Now I have a question for Serge: would it be better for the optimizer if no
compound statement were used an the "Diff" variable replaced in the calls
to TIMESTAMPDIFF with the expression above?
--
Knut Stolze
DB2 Information Integration Development
IBM Germany
I was wondering *exactly* the same thing. :)
I went for the block because it's a little clearer, and in general, i
do not duplicating expressions because it is easier to maintain.
B.
Oh, i didn't realize subtract resulted in a simple format, which is why
i used TIMESTAMPDIFF() to split it for me.
Your FUNCTION is mcuh easier to read, i think I'll switch. Thanx.
I do prefer TIME, if only because the two parameters are TIME. It just
kindof makes sense.
B.
> substr(digits(T ime_1 - Time_2),1,2) || ':' || substr(digits(T ime_1 - Time_2),3,2) || ':' || substr(digits(T ime_1 - Time_2),5,2)
There are many ways to convert format.here is another example.
TRANSLATE('AB:C D:EF', CHAR(Time_1 - Time_2), 'ABCDEF')
CHAR can be replaced by DIGITS.
That's a good one. I never thought of using TRANSLATE for formatting.
I'd prefer DIGITS here, if only because it is more specific than CHAR.
B.
Apparently Raise_Error uses an external call.
db2 => CREATE FUNCTION A() RETURNS INT NO EXTERNAL ACTION RETURN
Raise_Error('', '')
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0374N The "EXTERNAL ACTION" clause has not been specified in the
CREATE
FUNCTION statement for LANGUAGE SQL function "<snip>.A" but an
examination of the
function body reveals that it should be specified. LINE NUMBER=1.
SQLSTATE=428C2
B. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew |
last post by:
1) I have mySQL 3.23 installed. I have a record with two date/times
fields in YYYY-MM-DD HH:MM:SS format. How do I find the difference
between those two date/times? (I would also like to know it with
seconds left off.)
2) Let's say I have two records each with a date/time field in
YYYY-MM-DD HH:MM:SS format. How do I find the difference in date/times
when each field is in a different record?
Thanks,
|
by: Mac |
last post by:
$ cat junk27.c
#include <stdio.h>
#include <string.h>
int main (void)
{
printf("The difference between memcpy and memmove is %ld\n",
(long int) memcpy - (long int) memmove);
return 0;
}
|
by: Rich |
last post by:
I was considering C# for developing a scientific application, but I have
noticed a ~30% difference between VC++ .NET and C# on the same machine,
under identical conditions:
double a = 0,b = 0, c = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
|
by: jamesyreid |
last post by:
Hi,
I'm really sorry to post this as I know it must have been asked
countless times before, but I can't find an answer anywhere.
Does anyone have a snippet of JavaScript code I could borrow which
calculated the difference in years and days between two dates, and
takes leap years into account?
I'm calculating the difference in the usual way, i.e....
|
by: thebjorn |
last post by:
For the purpose of finding someone's age I was looking for a way to
find how the difference in years between two dates, so I could do
something like:
age = (date.today() - born).year
but that didn't work (the timedelta class doesn't have a year
accessor).
I looked in the docs and the cookbook, but I couldn't find anything, so
| |
by: Arjen |
last post by:
Hi,
Whats the difference between Request.IsAuthenticated and
User.Identity.IsAuthenticated?
Thanks!
Arjen
|
by: HC |
last post by:
Hello, all, I started out thinking my problems were elsewhere but as I
have worked through this I have isolated my problem, currently, as a
difference between MSDE and SQL Express 2005 (I'll just call it
Express for simplicity).
I have, to try to simplify things, put the exact same DB on two
systems, one running MSDE and one running Express. Both have 2 Ghz
processors (one Intel, one AMD), both have a decent amount of RAM
(Intel system...
|
by: slinky |
last post by:
Anyone know how to calculate the difference between two times
displayed in two textboxes?
I'm starting out with two textboxes: "txtCallTimeBegins" &
"txtCallTimeEnds"
this yielded: 6/7/2007 2:40:50 PM & 6/7/2007 2:45:04 PM respectively.
First I have coverted these to two additional textboxes to hold only
the time, not the date using:
=TimeValue() & =TimeValue()
|
by: Anonymous |
last post by:
I am writing a method that will send a message to another object, based
on changes (if any) bewtween two sets of data.
I anticipate that this method will be called about 50k-100k times - i.e.
it needs to check if there has been any change in a set of data a very
large number of times.
The way I am currently thinking of implementing this is to store a local
copy of the set, and then check for a difference between the local copy
and the...
|
by: jonathan184 |
last post by:
Hi is there a way you could get the difference between two dates and times.
Is there a function in perl to do this already?
basically i got a script and i have a start time and end time. so basically
i want the difference between the start and end to give the duration in date and time.
Could somebody please help me out with this please.
Thanks in advance
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |