diffrence between two chacter fields. [message #689944] |
Wed, 07 August 2024 21:15 |
shahzad-ul-hasan
Messages: 634 Registered: August 2002
|
Senior Member |
|
|
create table sin (
id number,intime varchar2(20),otime varchar2(50),dated date);
Intime (the time in when an employee enter in school).
otime (the time out when an employee leave the school).
i want to calculate the time diffrence. how many hours a employee spent in school.
please advised.
|
|
|
|
Re: diffrence between two chacter fields. [message #689946 is a reply to message #689945] |
Thu, 08 August 2024 05:30 |
|
Littlefoot
Messages: 21823 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
As Michel said, you used wrong datatype. If it were DATE, you'd subtract these two values and get number of days between them (because that's what difference of two DATE datatype values returns). Then multiply it by 24 (as there are 24 hours in a day) to get number of hours.
For example:
SQL> CREATE TABLE SIN
2 (
3 id NUMBER,
4 intime DATE,
5 otime DATE
6 );
Table created.
SQL> INSERT INTO SIN (id, intime, otime)
2 VALUES (1,
3 TO_DATE ('08.08.2024 08:00', 'dd.mm.yyyy hh24:mi'),
4 TO_DATE ('08.08.2024 14:30', 'dd.mm.yyyy hh24:mi'));
1 row created.
SQL> SELECT otime - intime AS days,
2 ROUND ((otime - intime) * 24, 1) AS hours
3 FROM SIN;
DAYS HOURS
---------- ----------
,270833333 6,5
SQL>
|
|
|
Re: diffrence between two chacter fields. [message #689947 is a reply to message #689946] |
Mon, 12 August 2024 23:28 |
shahzad-ul-hasan
Messages: 634 Registered: August 2002
|
Senior Member |
|
|
here is is code i have write
DECLARE
A NUMBER;
tiff_image_dir VARCHAR2(80) := 'D:\pics\';
photo_filename VARCHAR2(80);
BEGIN
:system.message_level:='25';
select distinct ename ,desig,dept
into :sin.name, :sin.cls,:sin.d
from EMP
where EMP.eid=:ID and estat='ACTIVE';
BEGIN
SELECT intime,otime INTO :SIN.intime,:SIN.otime FROM SIN
WHERE
SIN.ID=:SIN.ID AND SIN.otime IS NULL;
:SIN.otime:=to_date(:SYSTEM.CURRENT_DATETIME,'DD-MON-YYYY HH24:MI:SS');
UPDATE SIN SET Sin.otime=:Sin.otime
WHERE
Sin.ID=:Sin.ID AND Sin.otime IS NULL;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
:Sin.intime:=to_date(:SYSTEM.CURRENT_DATETIME,'DD-MON-YYYY HH24:MI:SS');
:Sin.otime:=NULL;
INSERT INTO Sin(ID,DATED,intime)
VALUES
(:Sin.ID,:Sin.DATED,:Sin.intime);--,'DD/MM/YYYY HH24:MI:SS'));
COMMIT;
END;
:Sin.IID:=:Sin.ID;
EXCEPTION WHEN NO_DATA_FOUND THEN
--commit;
ROLLBACK;
CLEAR_FORM;
RAISE FORM_TRIGGER_FAILURE;
:system.message_level:='0';
END;
:Sin.ID:=NULL;
i have change the field to date .please see the attached file. kindly advise how i can pick the time also.
-
Attachment: 22.jpg
(Size: 46.42KB, Downloaded 278 times)
|
|
|
|
|