Home » SQL & PL/SQL » SQL & PL/SQL » diffrence between two chacter fields.
diffrence between two chacter fields. [message #689944] Wed, 07 August 2024 21:15 Go to next message
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 #689945 is a reply to message #689944] Thu, 08 August 2024 00:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
please advised

Use DATE datatype and not VARCHAR2 to store date/time data.

[Updated on: Thu, 08 August 2024 00:13]

Report message to a moderator

Re: diffrence between two chacter fields. [message #689946 is a reply to message #689945] Thu, 08 August 2024 05:30 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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)
Re: diffrence between two chacter fields. [message #689948 is a reply to message #689947] Tue, 13 August 2024 01:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

If you post a working Test case: create table and insert statements along with the result you want with these data then we be able to will work with your table and data.

Also always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.
Re: diffrence between two chacter fields. [message #689949 is a reply to message #689947] Tue, 13 August 2024 01:22 Go to previous message
Littlefoot
Messages: 21823
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
According to screenshot you posted (Oracle Forms, right?) and what you said ("i have change the field to date"), I'd say that you set it wrong. Should've been datetime, not just date:

/forum/fa/14806/0/
Previous Topic: specific text from the string
Next Topic: minimize the SELECT statements
Goto Forum:
  


Current Time: Thu Nov 21 07:39:07 CST 2024