×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Date arthmetic.

Date arthmetic.

Date arthmetic.

(OP)
Hi friends,

I am using IDS 7.31 on HP-D370 Server. In SQL query I would like to find out the time difference between two days. For. E.g Previous day say 19/04/02 22:30:00 to 20/04/02 06:30:00 Hrs. I would like to know the time difference i.e 8 Hrs, 9 Hrs etc.

I tried same thing in 4GL also, since the from time is more than to time I am not getting the proper reuslt.

Will you give me a solution.


G.R.P.

RE: Date arthmetic.

G.R.P:

There's many ways of handling this problem.  Since I never took the time to learn all the nuances of the Informix datetime stuff, my solution was creating this Stored Procedure:

-- This function returns the number of seconds between
--datetime dt1 and dt2.
-- change to character first because the interval to
-- integer conversion fails.  Ed Schaefer 09/23/98
CREATE PROCEDURE int_in_sec(dt1 DATETIME YEAR TO SECOND, dt2 DATETIME YEAR
TO SECOND) RETURNING INTEGER;
DEFINE int_sec INTERVAL SECOND(9) TO SECOND;
DEFINE xchar CHAR(20);
DEFINE xint INTEGER;

LET int_sec = dt2 - dt1;  -- get the interval
LET xchar = int_sec; -- change to char
LET xint = xchar; -- change to integer

RETURN xint;
END PROCEDURE;

Since you want days and not seconds, divide by 3600.  Here's an example:

create temp table dts (
  dt1 datetime year to second,
  dt2 datetime year to second
) with no log;
insert into dts values ("2000-04-19 22:30:00", "2000-04-20 22:30:00");
select dt1, dt2, int_in_sec(dt1, dt2)/3600 from dts;
drop table dts;

You could modify the S.P. to return days instead of seconds by changing the interval declaration to something like:

DEFINE int_days INTERVAL DAY(5) TO DAY

although I've never tested this.  If you choose to do this make sure whatever day range you choose i.e. DAY(5) is large enough to hold the largest number of days.

Regards,


Ed
Schaefer

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close