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;
Related
I have a table with a column that contained time duration of events.
It it formatted as 'h:mm:ss'
I found the function strftime - but according to the manual, it requires the format 'hh:mm:ss'
can someone tell me how i can sum up the duration without recreating the sql table?
Is this what you want ?
with t as (
select '4:02:01' as v
union all
select '9:30:12'
union all
select '2:14:00'
),
diff as (
select sum(strftime('%s', '0'||v) - strftime('%s', '00:00:00')) as v
from t
)
select (v/3600) || ' hours, ' || (v%3600/60) ||' minutes, '
|| (v%60) || ' seconds.'
from diff
https://www.db-fiddle.com/f/2SNbFYQv2zYiCE4gw5bhzi/0
You can use time() and strftime():
select
time(sum(
strftime('%s', case length(timecolumn) when 7 then '0' else '' end || timecolumn)
- strftime('%s','00:00:00')),
'unixepoch') totaltime
from tablename
The result time sum will be in format hh:mm:ss.
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
);
Hi i am getting an error as expected something like END keyword between DATEDIFF and (
for the below statement under
select
case when CC.CASE_STS_CD in ( 'Closed', 'Auto Closed') then
DATEDIFF(second,CC.REC_DTTM_PST,CC.CRT_DTTM_PST) end as CASE_RES_DUR_IN_SECS,
Assuming that your fields are DATE datatype (otherwise you'll need to cast):
SELECT
CASE WHEN
CC.CASE_STS_CD IN ('Closed','Auto Closed') THEN
(CC.REC_DTTM_PST - CC.CRT_DTTM_PST) * 86400
END AS CASE_RES_DUR_IN_SECS
There's no DATEDIFF function in Teradata.
This is a generic SQL UDF I wrote a few years ago for calculating the difference of two timestamps in seconds:
REPLACE FUNCTION TimeStamp_Diff_Seconds
(
ts1 TIMESTAMP(6)
,ts2 TIMESTAMP(6)
)
RETURNS DECIMAL(18,6)
LANGUAGE SQL
CONTAINS SQL
RETURNS NULL ON NULL INPUT
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN
(CAST((CAST(ts2 AS DATE)- CAST(ts1 AS DATE)) AS DECIMAL(18,6)) * 60*60*24)
+ ((EXTRACT( HOUR FROM ts2) - EXTRACT( HOUR FROM ts1)) * 60*60)
+ ((EXTRACT(MINUTE FROM ts2) - EXTRACT(MINUTE FROM ts1)) * 60)
+ (EXTRACT(SECOND FROM ts2) - EXTRACT(SECOND FROM ts1))
;
I got parameter
:dateFrom
which gonna be used as an argument in a function as a TIMESTAMP. I need to add to :dateFrom + 7 hours, how can I do that?
If your parameter is not already a timestamp, use to_timestamp or to_date to convert it:
to_timestamp(dateFrom,'mm/dd/yyyy hh24:mi:ss')
(substitute the appropriate mask based on the format of your input parameter)
Then just add 7/24.
to_timestamp(dateFrom,'mm/dd/yyyy hh24:mi:ss') + 7/24;
Adding 1 adds a full day, so adding 1/24 adds 1 hour.
This can also be done with the INTERVAL operator:
to_timestamp(dateFrom,'mm/dd/yyyy hh24:mi:ss') + INTERVAL '7' hour
Here is some PL/SQL that will:
DECLARE
dateFrom TIMESTAMP;
BEGIN
dateFrom := SYSTIMESTAMP;
DBMS_OUTPUT.PUT_LINE('BEFORE :: ' || dateFrom);
dateFrom := dateFrom + INTERVAL '2' HOUR;
DBMS_OUTPUT.PUT_LINE('AFTER :: ' || dateFrom);
END;
/
Look into the INTERVAL operator.
You can do like this,
select dateFrom + interval '7' hours from dual
In the arithmetic of the dates, in Oracle, when you add a number, it is intended as NUMBER OF DAYS.
7 hours are 7/24 days, so you have simply to add 7/24.
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;
/