Home » SQL & PL/SQL » SQL & PL/SQL » Time Datatype (Oracle 10g)
|
Re: Time Datatype [message #596346 is a reply to message #596345] |
Sat, 21 September 2013 02:35 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
sss111ind wrote on Sat, 21 September 2013 12:33
How to maintain only the time in a column .Is there any time datatype.
Now we are maintaining like in number eg for 08:15:34 as 081534 in a number datatype.
T.Kyte says - Never never use a number or character string to store that which has a native format like
a DATE. Don't use two fields either -- they always go together and you can always mask
one or the other off for display.
+1 to him.
The bottomline is, there is no such data type which will allow you to store ONLY the time. Even if you try to insert(I know you would have tried it by now), you would end up with a DATE followed by time always, so it has to be together ALWAYS.
Below code demonstrates that it's impossible.
SQL> DROP TABLE T;
Table dropped
SQL> CREATE TABLE T(TIME_COL TIMESTAMP(6));
Table created
SQL> INSERT INTO T VALUES(TO_TIMESTAMP('12:12:12','HH:MI:SS'));
1 row inserted
SQL> COMMIT;
Commit complete
SQL> SELECT * FROM T;
TIME_COL
-------------------------------------------------
01-SEP-13 12.12.12.000000 PM
So, store as DATE, and display using TO_CHAR in your desired format.
Regards,
Lalit
[Updated on: Sat, 21 September 2013 02:49] Report message to a moderator
|
|
|
|
Re: Time Datatype [message #596350 is a reply to message #596345] |
Sat, 21 September 2013 03:31 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Can you use the interval data type?orclz> create table t1(c1 interval day to second,c2 varchar2(20));
Table created.
orclz> insert into t1 values(interval '90' minute,'01:30');
1 row created.
orclz> select * from t1;
C1 C2
--------------------------------------------------------------------------- -----------
+00 01:30:00.000000 01:30
orclz>
|
|
|
Re: Time Datatype [message #596351 is a reply to message #596350] |
Sat, 21 September 2013 05:34 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
There are two columns like start_time is 10:00 AM and end_time is 07:00. So everyday employee entries needs to inserted into the table.If they are failing to come in with that range attendance will not be calculated.
And for that now we have an entry with number start_time is 1000 and end_time is 1900.Please suggest for this.
[Updated on: Sat, 21 September 2013 05:40] Report message to a moderator
|
|
|
|
Re: Time Datatype [message #596362 is a reply to message #596352] |
Sat, 21 September 2013 09:52 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
In number datatype case we are validating the things like
WITH TEMP AS
(SELECT TO_DATE('21-09-2013 10:23:11', 'DD-MM-YYYY HH24:MI:SS') entry_time,
TO_DATE('21-09-2013 18:59:51', 'DD-MM-YYYY HH24:MI:SS') exit_time
FROM DUAL
UNION ALL
SELECT TO_DATE('21-09-2013 13:24:31', 'DD-MM-YYYY HH24:MI:SS') entry_time,
TO_DATE('21-09-2013 19:33:10', 'DD-MM-YYYY HH24:MI:SS') exit_time
FROM DUAL
UNION ALL
SELECT TO_DATE('21-09-2013 10:00:51', 'DD-MM-YYYY HH24:MI:SS') ENTRY_TIME,
TO_DATE('21-09-2013 19:00:43', 'DD-MM-YYYY HH24:MI:SS') EXIT_TIME
FROM DUAL)
SELECT *
FROM TEMP
WHERE to_char(entry_time, 'hh24miss') >= 100000
and to_char(exit_time, 'hh24miss') <= 190000;
In where condition 100000 represnts as 10:00:00 AM start time and 19:00:00 pm represents end time.But for Michel's case I do not in which way I can validate.Please give some idea.
[Updated on: Sat, 21 September 2013 09:52] Report message to a moderator
|
|
|
Re: Time Datatype [message #596364 is a reply to message #596362] |
Sat, 21 September 2013 11:08 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
sss111ind wrote on Sat, 21 September 2013 16:52In number datatype case we are validating the things like
WITH TEMP AS
(SELECT TO_DATE('21-09-2013 10:23:11', 'DD-MM-YYYY HH24:MI:SS') entry_time,
TO_DATE('21-09-2013 18:59:51', 'DD-MM-YYYY HH24:MI:SS') exit_time
FROM DUAL
UNION ALL
SELECT TO_DATE('21-09-2013 13:24:31', 'DD-MM-YYYY HH24:MI:SS') entry_time,
TO_DATE('21-09-2013 19:33:10', 'DD-MM-YYYY HH24:MI:SS') exit_time
FROM DUAL
UNION ALL
SELECT TO_DATE('21-09-2013 10:00:51', 'DD-MM-YYYY HH24:MI:SS') ENTRY_TIME,
TO_DATE('21-09-2013 19:00:43', 'DD-MM-YYYY HH24:MI:SS') EXIT_TIME
FROM DUAL)
SELECT *
FROM TEMP
WHERE to_char(entry_time, 'hh24miss') >= 100000
and to_char(exit_time, 'hh24miss') <= 190000;
In where condition 100000 represnts as 10:00:00 AM start time and 19:00:00 pm represents end time.But for Michel's case I do not in which way I can validate.Please give some idea.
1/ "to_char(entry_time, 'hh24miss') >= 100000" you compare a string with a number?
2/ Who say there will not be 1888888 or '188888' or 'MICHEL' somewhere?
3/ I already told you the best way
Regards
Michel
|
|
|
Re: Time Datatype [message #596423 is a reply to message #596364] |
Mon, 23 September 2013 01:43 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
Hi All,
I WANT BASICALLY THAT if some EMPLOYEE WHICH BELONG to IT COMES BEYOND the SPECIFIED range then IT WILL MARKED as INVALID STATUS WHILE INSERTING a RECORD.
How I can convert this into Michels case.Please give some input.
create table dept_time(start_time number,end_time number,dept varchar2(50));
insert into DEPT_TIME values (100000,190000,'SALESS');
insert into DEPT_TIME values(90000,180000,'FINANCE');
insert into dept_time VALUES(113000,203000,'IT');
insert into dept_time VALUES(143000,233000,'SUPPORT');
create or replace procedure validate_entry_time(p_entry_time date default sysdate ,p_dept in varchar2,p_status out varchar2) is
l_entry_time number;
l_start_time number;
l_end_time number;
l_dept varchar2(50);
begin
l_entry_time:=to_number(to_char(sysdate,'hh24miss'));
select start_time,end_time,dept into l_start_time,l_end_time,l_dept from dept_time where dept=p_dept;
if l_entry_time not between l_start_time and l_end_time then
p_status:='Invalid';
ELSE
p_status:='Valid';
end if;
end;
declare
p_status varchar2(100);
begin
validate_entry_time(null,'IT',p_status);
dbms_output.put_line('status ' ||p_status);
end;
[Updated on: Mon, 23 September 2013 01:46] Report message to a moderator
|
|
|
|
Re: Time Datatype [message #596427 is a reply to message #596425] |
Mon, 23 September 2013 02:03 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
create table dept_time(start_time number,end_time number,dept varchar2(50));
insert into DEPT_TIME values (100000,190000,'SALESS');
insert into DEPT_TIME values(90000,180000,'FINANCE');
insert into dept_time VALUES(113000,203000,'IT');
insert into dept_time VALUES(143000,233000,'SUPPORT');
CREATE OR replace PROCEDURE Validate_entry_time(
p_entry_time DATE DEFAULT SYSDATE,
p_dept IN VARCHAR2,
p_status OUT VARCHAR2)
IS
l_entry_time NUMBER;
l_start_time NUMBER;
l_end_time NUMBER;
l_dept VARCHAR2(50);
BEGIN
l_entry_time := To_number(To_char(SYSDATE, 'hh24miss'));
SELECT start_time,
end_time,
dept
INTO l_start_time, l_end_time, l_dept
FROM dept_time
WHERE dept = p_dept;
IF l_entry_time NOT BETWEEN l_start_time AND l_end_time THEN
p_status := 'Invalid';
ELSE
p_status := 'Valid';
END IF;
END;
declare
p_status varchar2(100);
begin
validate_entry_time(null,'IT',p_status);
dbms_output.put_line('status ' ||p_status);
end;
|
|
|
|
|
Re: Time Datatype [message #596431 is a reply to message #596430] |
Mon, 23 September 2013 02:49 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Your type conversions are corrupting the data. For instance: orclz>
orclz> select sysdate from dual;
SYSDATE
-------------------
2013-09-23 08:47:08
orclz> select (To_char(SYSDATE, 'hh24miss')) from dual;
(TO_CH
------
084718
orclz> select To_number(To_char(SYSDATE, 'hh24miss')) from dual;
TO_NUMBER(TO_CHAR(SYSDATE,'HH24MISS'))
--------------------------------------
84721
orclz>
You would need to compare the values as strings, to_number loses leading zeros.
|
|
|
|
|
Re: Time Datatype [message #596446 is a reply to message #596438] |
Mon, 23 September 2013 05:23 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
The purpose of procedure is validating employees entry or exit timing.
If anything misses those boundaries then the status field marked as invalid against the record
which is inserted into the employee_time table else valid .The table is like as follows.
--employee_time (entry_time date,exit_time date,status varchar2(10),emp_id varchar2(30))
DECLARE
p_status VARCHAR2(100);
p_emp_id VARCHAR2(30);
BEGIN
INSERT INTO employee_time(entry_time,exit_time, status,emp_id)
VALUES (SYSDATE, NULL, NULL,p_emp_id);--first time emtry
Validate_entry_time(NULL, 'IT', p_status);
UPDATE employee_time SET status = p_status,exit_time = SYSDATE
WHERE employee_id = p_emp_id; --status updating for entyr or exit
END;
[Updated on: Mon, 23 September 2013 05:31] Report message to a moderator
|
|
|
|
Re: Time Datatype [message #596459 is a reply to message #596446] |
Mon, 23 September 2013 07:17 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
sss111ind wrote on Mon, 23 September 2013 15:53The purpose of procedure is validating employees entry or exit timing.
Well, there is no employee column to validate for each employee. The output will always be for an entire department. There should be a column for employees too.
Let the dept_time table be one time created with the values as of created date appended with the cut off times of entry and exit, finally stored as timestamp data type.
I tried this, see if helps in anyway -
SQL> CREATE TABLE dept_time
2 (
3 start_time TIMESTAMP,
4 end_time TIMESTAMP,
5 dept VARCHAR2(50)
6 );
Table created.
SQL>
SQL> INSERT INTO dept_time
2 VALUES (To_timestamp(Trunc(SYSDATE)
3 ||' '
4 ||'10:00:00 AM'),
5 To_timestamp(Trunc(SYSDATE)
6 ||' '
7 ||'7:00:00 PM'),
8 'SALES');
1 row created.
SQL>
SQL> INSERT INTO dept_time
2 VALUES (To_timestamp(Trunc(SYSDATE)
3 ||' '
4 ||'7:00:00 AM'),
5 To_timestamp(Trunc(SYSDATE)
6 ||' '
7 ||'4:00:00 PM'),
8 'FINANCE');
1 row created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
SQL> SELECT dept,
2 To_char(SYSDATE, 'hh12:mi:ss AM') curr_time,
3 CASE
4 WHEN systimestamp BETWEEN To_timestamp(Trunc(SYSDATE)
5 ||' '
6 ||To_char(start_time,
7 'hh12:mi:ss AM'))
8 AND
9 To_timestamp(
10 Trunc(SYSDATE)
11 ||' '
12 ||
13 To_char(end_time, 'hh12:mi:ss AM'
))
14 THEN
15 'valid'
16 ELSE 'invalid'
17 END status
18 FROM dept_time;
DEPT CURR_TIME STATUS
-------------------------------------------------- ----------- -------
SALES 05:55:42 PM valid
FINANCE 05:55:42 PM invalid
Regards,
Lalit
[Updated on: Mon, 23 September 2013 07:26] Report message to a moderator
|
|
|
|
|
Re: Time Datatype [message #596469 is a reply to message #596466] |
Mon, 23 September 2013 11:29 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Michel Cadot wrote on Mon, 23 September 2013 13:06
1/
There is no relation between the current employee and your procedure but its department which means all employees of each department will have the same status.
2/
There is no reason to use PL/SQL for this which can be done in SQL with UPDATE + INSERT statements.
|
|
|
Goto Forum:
Current Time: Thu Apr 25 14:09:55 CDT 2024
|