Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Raihan.

Asked: April 19, 2019 - 2:01 am UTC

Last updated: April 24, 2019 - 2:43 pm UTC

Version: 11g

Viewed 1000+ times

You Asked

Hello there,
I want to create a trigger that will insert a
Time difference value into a table
Example:
I have attendance table
Sign_in date;
Sign_out date;
Late_in number;
Early_out number;
Now I want to create a trigger that will insert late time of a employee, suppose if a employee sign in after 9:00 am the trigger will calculate the difference between sign_in and 9:00 am and then insert the difference in late_in.
Anyone else can help me?


and Chris said...

Rather than have a trigger to store the value, I'd make late_in/early_out virtual columns. These include the expression that calculates the difference between the sign_in/out datetimes and the cut-off values. And are calculated at runtime. So guaranteed to stay in sync with the source time if you update it.

There are various techniques you could use to find the difference. Here's one:

- Get 9am on the date of the sign in by:
- Truncing the value to get the date at midnight
- Add 9 hours to this to have 9am that day
- Subtract this from the sign-in, converting to hours/minutes as needed
- (optional: ignore early sign-ins by returning the greatest of the above expression and zero

Which gives:

create table t (
  sign_in         date,
  late_in_minutes number as (
    greatest ( 
      round ( ( sign_in - ( trunc ( sign_in ) + 9/24 ) ) * 1440 ), 
      0
    )
  )
);

insert into t ( sign_in ) 
  values ( date'2019-04-24' + 8/24 );
insert into t ( sign_in ) 
  values ( date'2019-04-24' + 9/24 );
insert into t ( sign_in ) 
  values ( date'2019-04-24' + 9.1/24 );
insert into t ( sign_in ) 
  values ( date'2019-04-24' + 10/24 );
  
select * from t;

SIGN_IN                LATE_IN_MINUTES   
24-APR-2019 08:00:00                     0 
24-APR-2019 09:00:00                     0 
24-APR-2019 09:06:00                     6 
24-APR-2019 10:00:00                    60 

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Raihan Rana, April 26, 2019 - 12:44 pm UTC

It works , so many thanks

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.