My requirement is need to convert the input date values which is in yyyy-MM-dd (user input value) format to GMT and we have our own timezone table which stores all the timezones values. So we need to convert input value(yyyy-MM-dd) to GMT as we are storing the values in GMT only. I have tried with FROM_TZ, CAST functions but getting timezone region not found issue.
Can anyone please suggest the changes accordingly.
Here attaching the some sample code (this is user input value).
start_time >= to_date(to_char((from_tz(to_timestamp('2017-09-01', 'yyyy-MM-dd HH:MI:SS AM') , tz.offSet) at time zone 'GMT'),'MM/dd/yyyy HH:MI AM'),'MM/dd/yyyy HH:MI AM')
Thanks,
Sant
I did this in my case :
-- returns a date value of a date converted in UTC and formated
cst_timestamp_fmt constant varchar2(20) := 'DD-MON-YY HH24:MI:SS';
function fn_Date_To_UTC_Formated(p_date date) return date is
begin
return to_date(to_char(
to_timestamp(
sys_extract_utc(
to_timestamp(
to_char(p_date,
cst_timestamp_fmt
),
cst_timestamp_fmt
)
)
),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'
);
end fn_Date_To_UTC_Formated;
and to test it:
declare
cst_timestamp_fmt constant varchar2(20) := 'DD-MON-YY HH24:MI:SS';
function fn_Date_To_UTC_Formated(p_date date) return date is
begin
return to_date(to_char(
to_timestamp(
sys_extract_utc(
to_timestamp(
to_char(p_date,
cst_timestamp_fmt
),
cst_timestamp_fmt
)
)
),
'YYYY-MM-DD"T"HH24:MI:SS"Z"'),'YYYY-MM-DD"T"HH24:MI:SS"Z"'
);
end fn_Date_To_UTC_Formated;
begin
dbms_output.put_line(' sysdate : '||to_char(sysdate, cst_timestamp_fmt));
dbms_output.put_line(' result : '||to_char(fn_Date_To_UTC_Formated(sysdate),cst_timestamp_fmt));
end;
result is
sysdate : 20-SEP-17 15:20:09
result : 20-SEP-17 13:20:09
hope that can help.
Related
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
);
When I try to insert datetime value 12:58 AM into the oracle table it gets inserted as 00:58. How can I insert datetime value as 12 in my oracle db? I've set my Oracle time format as 24 hr time. Any suggestions would help.
Insert statement :
INSERT INTO TABLE
(
DATE_CREATED,
PLANNED_START,
PLANNED_COMPLETION
)
VALUES
(
sysdate,
TO_CHAR(p_planned_Start_Date, 'DD-MM-YYYY HH24:MI:SS'),
TO_CHAR(end_date_, 'DD-MM-YYYY HH24:MI:SS')
);
For the 24-hour time, you need to use HH24 instead of HH.
For the 12-hour time, the AM/PM indicator is written as A.M. (if you want periods in the result) or AM (if you don't). For example:
INSERT INTO TEST (LD_DATE) Values (TO_DATE('08/30/2016', 'MM/DD/YYYY '));
And select it as below:
SELECT LD_DATE,
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH24:MI:SS') "Date 24Hr",
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH:MI:SS AM') "Date 12Hr"
FROM test
;
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.
am wondering if there already some rewriting suggestions to get functions such generating recurrence dates between two dates - generate_recurrences() from this link for recurrency recurrency events
in plsql? it returns a setof date, but in plsql i can't figure out how to get a resultset for dates and looping return next next_date, where next also returns a next date on a list.
I tried it to rewrite it in plsql but with only return of one date, because i can't find out how to return a resultset in plsql, that what i've tried:
CREATE OR REPLACE FUNCTION GENERATE_RECURRENCE( rec in VARCHAR2,
start_date in TIMESTAMP,
end_date in TIMESTAMP )
RETURN TIMESTAMP
IS
next_date TIMESTAMP := start_date;
duration INTERVAL DAY TO SECOND;
day INTERVAL DAY TO SECOND;
BEGIN
IF recurs = 'none' THEN
return next_date;
elsif recurs = 'daily' then
duration := INTERVAL '1' DAY ;
while next_date <= end_date loop
return next_date + duration;
END IF;
END;
I wrote the following pipelined function a while ago. It's not exactly what you're asking for, but it gives you a resultset that's a range of dates, so you should be able to match it to your needs.
It requires you to create a type object to hold the return value and I used an existing object instead of creating a custom one. So you should modify this to use an object just big enough (and use date type instead of string). But the functionality does what you're asking for.
Enjoy!
CREATE OR REPLACE FUNCTION date_range_stream(start_date_in IN DATE,
end_date_in IN DATE) RETURN rpt_results_10_obj_type_type
DETERMINISTIC
PIPELINED IS
/*
Parameters: start_date_in - First date to return (truncated)
end_date_in - Last date to return, inclusive
Results: date string formatted as MM/DD/YYYY
Author: Stew Stryker
Usage: SELECT to_date(text01, 'MM/DD/YYYY') AS a_date
FROM TABLE(aeo.aeo_misc_tools.date_range_stream('01-MAR-2009', SYSDATE))
Returns a rows from starting date to current
Requires the definition of the following object:
CREATE OR REPLACE TYPE rpt_results_10col_obj AS OBJECT
( seq_num NUMBER,
place VARCHAR2(20),
rep_info VARCHAR2(20),
text01 VARCHAR2(512),
text02 VARCHAR2(512),
text03 VARCHAR2(512),
text04 VARCHAR2(512),
text05 VARCHAR2(512),
text06 VARCHAR2(512),
text07 VARCHAR2(512),
text08 VARCHAR2(512),
text09 VARCHAR2(512),
text10 VARCHAR2(512));
*/
cur_date DATE := trunc(start_date_in);
date_row rpt_results_10col_obj := aeo.rpt_results_10col_obj(NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL);
BEGIN
WHILE cur_date <= trunc(end_date_in)
LOOP
date_row.text01 := TO_CHAR(cur_date, 'MM/DD/YYYY');
PIPE ROW(date_row);
cur_date := cur_date + 1;
END LOOP;
RETURN;
EXCEPTION
WHEN no_data_found THEN
RETURN;
WHEN OTHERS THEN
dbms_output.put_line('EXCEPTION IN aeo.aeo_misc_tools.date_range_stream - ' || SQLCODE || ': ' ||
SQLERRM);
RAISE;
RETURN;
END date_range_stream;
can i use to_number(to_char()) function in order to exclude all the weekends from a range of dates?
For instance, there are two date columns in my table such as start and finish (in the form of '06/06/2011 10:00:00 am'), and i want to estimate the duration of finish-start excluding Saturdays and Sundays.
If I understand you right you want to calculate the difference between two dates, but exclude the 2 days of each weekend in the range from the result. Is that correct?
If this is what you want the below code should work with the following assumptions:
I am assume start and end will not be on weekends.
I am not validating that end is before start.
Basically its just a matter of working out how many weekends are in the date range. So obviously there's one weekend per 7 days. Then we just have to check if the range wraps around a weekend, and if so add one more.
FUNCION dateDiff( dt_start DATE, dt_end DATE ) RETURN NUMBER
IS
raw_diff NUMBER;
weekends NUMBER;
BEGIN
raw_diff := dt_end - dt_start;
weekends := TRUNC( raw_diff / 7 );
IF( ( dt_start - TRUNC( dt_start, 'DAY' ) )
> ( dt_end - TRUNC( dt_end , 'DAY' ) ) )
THEN
weekends := weekends + 1;
END IF;
RETURN raw_diff - ( weekends * 2 );
END;