I've this statement:
create table times (time_in timestamp, time_out timestamp);
insert into times values(to_timestamp('02-MAY-11 07.57.00.000000 AM'), to_timestamp('02-MAY-11 07.30.00.000000 PM'));
select extract(hour from(time_out-time_in))||':'||extract(minute from(time_out-time_in)) from times;
EXTRACT(HOURFROM(TIME_OUT-TIME_IN))||':'||EXTRACT(MINUTEFROM(TIME_OUT-TIME_IN))
---------------------------------------------------------------------------------
11:33
And now, I wanna compare the result above. For example:IF [result] > [8 hours] THEN ...
How is to do that?
SELECT *
FROM times
WHERE time_out - time_in > interval '8' hour
Inside a PL/SQL procedure this would look like this:
declare
result interval day to second;
begin
-- !!! make sure this select returns only one row, or use a cursor !!!
select time_out - time_in
into result
from times;
if (result > interval '8' hour) then
dbms_output.put_line('greater');
end if;
end;
/
Related
Im trying to add hours to midnight of today eg: like 27 hours
I have tried various methods from the internet but am getting the trunc of the dated expected. eg 23-nov-2022 not 23-nov-2022 03:00. when i run it outside my pl/sql procedure/block i get the desired output
the select:
select to_char(to_date(sysdate,'DD-MON-RRRR HH:MI')+hours/24,'DD-MON-RRRR HH:MI') into v_from from dual;
I need some expert assistance
Add an INTERVAL DAY TO SECOND data type to SYSDATE TRUNCated back to midnight:
DECLARE
v_from DATE;
BEGIN
SELECT TRUNC(sysdate) + INTERVAL '27' HOUR
INTO v_from
FROM DUAL;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_from,'DD-MON-RRRR HH:MI'));
END;
/
or, more simply:
DECLARE
v_from DATE;
BEGIN
v_from := TRUNC(sysdate) + INTERVAL '27' HOUR;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_from,'DD-MON-RRRR HH:MI'));
END;
/
or, dynamically, with the NUMTODSINTERVAL function:
DECLARE
v_from DATE;
v_hours NUMBER(3,0) := 27;
BEGIN
v_from := TRUNC(sysdate) + NUMTODSINTERVAL(v_hours,'HOUR');
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_from,'DD-MON-RRRR HH:MI'));
END;
/
fiddle
Trunc SYSDATE to get midnight. Then add hours: 1 hour is 1/24 day so you'd add the number of hours divided by 24. Example.
koen>DECLARE
2 l_date DATE;
3 l_hours NUMBER := 27;
4 BEGIN
5 l_date := TRUNC(SYSDATE) + 27/24;
6 dbms_output.put_line('l_date is: '||TO_CHAR(l_date,'DD-MON-YYYY HH24:MI'));
7 END;
8* /
l_date is: 23-NOV-2022 03:00
PL/SQL procedure successfully completed.
koen>
The select from dual is not advised, you can just assign a variable in pl/sql using the assignment operator :=. The select from dual requires an additional context switch (invoke the sql engine from within pl/sql).
I want to replace '02:00' with a variable in the below code in PLSQL procedure.
SELECT SYSTIMESTAMP + interval '02:00' HOUR TO MINUTE
INTO est_close_date
FROM DUAL;
Define the variable as the number of hours, then you can do:
declare
l_number_of_hours integer;
...
begin
...
l_number_of_hours := 6;
SELECT SYSTIMESTAMP + interval '1' HOUR * l_number_of_hours
INTO est_close_date
FROM DUAL;
end;
If you need finer granularity, use minutes:
declare
l_number_of_minutes integer;
...
begin
...
l_number_of_minutes := 2 * 60 + 30;
SELECT SYSTIMESTAMP + interval '1' minute * l_number_of_minutes
INTO est_close_date
FROM DUAL;
end;
I have this anonymous block:
DECLARE
V_DATA DATE;
BEGIN
V_DATA := '01-GEN-2000';
HR.STATISTICHE.RATINGOPERATORI (V_DATA);
COMMIT;
END;
but I would to generate the date in a random way. How can I do?
You can generate random dates between two dates ,as displayed in the query below .Random Dates are generated between 1-jan-2000 and 31-dec-9999
SELECT TO_DATE(
TRUNC(
DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J')
,TO_CHAR(DATE '9999-12-31','J')
)
),'J'
) FROM DUAL;
OR you can use
SELECT TO_DATE (
TRUNC (
DBMS_RANDOM.VALUE (2451545, 5373484)
)
, 'J'
)
FROM DUAL
In the above example ,the first value is 01-Jan-2000 and the second value id 31-dec-9999
To generate random date you can use
select to_date('2010-01-01', 'yyyy-mm-dd')+trunc(dbms_random.value(1,1000)) from dual
or for random datetime
select to_date('2010-01-01', 'yyyy-mm-dd')+dbms_random.value(1,1000) from dual
If you want to see it's logic, you can also use this code.
create or replace procedure genDate(result out nvarchar2) IS
year number;
month number;
day number;
Begin
year:=FLOOR(DBMS_RANDOM.value(2000,2100));
month:=FLOOR(DBMS_RANDOM.value(1,12));
IF month=2 and (year/4)=0 and (year/100)!=0 then
day:=FLOOR(DBMS_RANDOM.value(1,29));
ELSIF month=2 or (year/100)=0 then
day:=FLOOR(DBMS_RANDOM.value(1,28));
ELSIF MOD(month,2)=1 then
day:=FLOOR(DBMS_RANDOM.value(1,31));
ELSIF MOD(month,2)=0 and month!=2 then
day:=FLOOR(DBMS_RANDOM.value(1,30));
END IF;
result:=month||'-'||day||'-'||year;
End;
here is one more option to generate date going back from now where 365 - days quanitity to move back from today, 'DD.MM.YYYY'- mask
to_char(sysdate-dbms_random.value()*365, 'DD.MM.YYYY')
I needed to generate employee data for testing. Each employee needed a date of birth that put them between 16 and 65 years of age, and a date of hire sometime between their 16th birthday and SYSDATE. Here's how...
FUNCTION randomDateInRange(alpha IN DATE, omega IN DATE) RETURN DATE IS
BEGIN
RETURN alpha + DBMS_RANDOM.VALUE(0, omega - alpha);
END;
...and then, to use this function...
-- an employee can be any age from 16 to 65 years of age
DoB := randomDateInRange(
SYSDATE - INTERVAL '65' YEAR,
SYSDATE - INTERVAL '16' YEAR
);
-- an employee could have been hired any date since their sixteenth birthday
DoH := randomDateInRange(
DoB + INTERVAL '16' YEAR,
SYSDATE
);
I am trying to find the difference between two dates (Currentdate-Duedate)
I used Julianday('now')-Julianday(T1.DD), the results appear fine when the result are positive (when currentDate > DueDate), but results are incorrect when DueDate > Current Date (negative results appear 1 day lower than expected).
Following statement was tried:
Select( CAST((Julianday('now')-Julianday(T1.DD)) As Integer)
Example format of the column DD in Database 2017-07-21 00:00:00.
select case when julianday('now') > julianday(T1.DD)
then cast (julianday('now') - julianday(T1.DD) as int)
else cast ((julianday(T1.DD) - julianday('now') + 1) as int)
end
from test;
UPDATE 1:
select cast(julianday('now') as int) - cast(julianday(T1.DD) as int)
from test;
UPDATE 2:
select cast(julianday(date('now') )- julianday(date(T1.DD)) as int )
from test;
I have a column in my table (Data_type Number(16,0)) which stores the epoch time with milliseconds.
Eg :- 1491456096759
I want to write a select statement to format this into readable format which includes milliseconds 'YYYY-MM-DD HH24:MI:SS.FF'.
Is there any direct functions that i can use for this conversion??
I have already tried this option
select to_date('19700101', 'YYYYMMDD') + ( 1 / 24 / 60 / 60 / 1000) * 1491456096759
from dual;
But not able to print that date in 'YYYY-MM-DD HH24:MI:SS.FF' format
Please try this:
SELECT TO_TIMESTAMP('1970-01-01 00:00:00.0'
,'YYYY-MM-DD HH24:MI:SS.FF'
) + NUMTODSINTERVAL(1493963084212/1000, 'SECOND')
FROM dual;
Or this if you want to return a string:
SELECT TO_CHAR(
TO_TIMESTAMP('1970-01-01 00:00:00.0'
,'YYYY-MM-DD HH24:MI:SS.FF'
) + NUMTODSINTERVAL(1493963084212/1000, 'SECOND')
,'YYYY-MM-DD HH24:MI:SS.FF')
FROM dual;