Subject | Time difference in minutes/seconds/whatever |
---|---|
Author | PenWin |
Post date | 2008-10-02T09:40:33Z |
Hi!
What is the proper way to calculate a time difference in minutes
(seconds, hours, years...) between two timestamps? I am using Firebird
1.5 and I would very much like to manage this task just by using PL/SQL,
without resorting to UDFs.
Originally I wrote this procedure:
CREATE PROCEDURE diff (time1 TIMESTAMP, time2 TIMESTAMP)
RETURNS (min_diff INTEGER)
AS BEGIN
time1 = time1-time2;
min_diff = time1*60*24;
SUSPEND;
END
It does not work, complains when I try to compile it:
"SQL error code = -607
Array/BLOB/DATE data types not allowed in arithmetic"
So I modified it with CAST to DOUBLE PRECISION:
...
min_diff = CAST(time1 AS DOUBLE PRECISION)*60*24;
...
This compiles OK, but when I try to execute the procedure:
SELECT * FROM diff(field1, field2)
I get an error message:
"conversion error from string "0.013194444""
Now that value 0.013194444 corresponds to the expected difference
between the two fields I used. What I don't understand is why I am
getting this error and why it is a conversion from string. I suspect it
might be related to the fact that my Windows locale sets a different
decimal separator than ".", but everywhere else SQL works just fine with
the dot (as expected). Any ideas?
Thanks,
Pepak
What is the proper way to calculate a time difference in minutes
(seconds, hours, years...) between two timestamps? I am using Firebird
1.5 and I would very much like to manage this task just by using PL/SQL,
without resorting to UDFs.
Originally I wrote this procedure:
CREATE PROCEDURE diff (time1 TIMESTAMP, time2 TIMESTAMP)
RETURNS (min_diff INTEGER)
AS BEGIN
time1 = time1-time2;
min_diff = time1*60*24;
SUSPEND;
END
It does not work, complains when I try to compile it:
"SQL error code = -607
Array/BLOB/DATE data types not allowed in arithmetic"
So I modified it with CAST to DOUBLE PRECISION:
...
min_diff = CAST(time1 AS DOUBLE PRECISION)*60*24;
...
This compiles OK, but when I try to execute the procedure:
SELECT * FROM diff(field1, field2)
I get an error message:
"conversion error from string "0.013194444""
Now that value 0.013194444 corresponds to the expected difference
between the two fields I used. What I don't understand is why I am
getting this error and why it is a conversion from string. I suspect it
might be related to the fact that my Windows locale sets a different
decimal separator than ".", but everywhere else SQL works just fine with
the dot (as expected). Any ideas?
Thanks,
Pepak