Calculating the difference between two timestamps - oracle

I am trying to calculate the difference between to timestamps, but the output only returns the difference in days, but not time:
DECLARE
a INTERVAL DAY(2) TO SECOND(00);
TT DATE := TO_DATE('15-Nov-2015 10:00 am', 'dd-mon-yyyy hh:mi am');
TS DATE := TO_DATE('17-Nov-2015 12:12 am', 'dd-mon-yyyy hh:mi am');
BEGIN
--Compute interval and assign to an INTERVAL DAY TO SECOND variable
a := TO_TIMESTAMP(TS,'dd-Mon-yyyy hh:mi am')
- TO_TIMESTAMP(TT,'dd-Mon-yyyy hh:mi am');
DBMS_OUTPUT.PUT_LINE(a);
END;
This returns
+02 00:00:00

Use the following code
$start_date = new DateTime('2015-11-15 10:00:00');
$since_start = $start_date->diff(new DateTime('2015-11-17 12:12:25'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';
This code will output
The above code will output:
2 days total
0 years
0 months
2 days
2 hours
12 minutes
25 seconds
You can further calculate the time difference in minutes and seconds

DECLARE
a INTERVAL DAY(2) TO SECOND(0);
BEGIN
--Compute interval and assign to an INTERVAL DAY TO SECOND variable
a := TO_TIMESTAMP('17:00',' hh24:mi ')
- TO_TIMESTAMP('08:00',' hh24:mi ');
-- - TO_TIMESTAMP('20-SEP-2015 10:59 pm','dd-Mon-yyyy hh:mi am');
DBMS_OUTPUT.PUT_LINE(SUBSTR(a, 5, 6));
END;
This is the correct solution! Thanks for the attempted help!

Related

Oracle APEX Automation Schedule

Is it possible to exceute oracle apex automation on a specific day of the month(example every 28th of the month),,,once a month,,monthly? so far the settings only offer - weekly, daily,etc,
Is it possible to tweak through the Schedule Expression? (Example: FREQ=DAILY;INTERVAL=1;BYHOUR=23;BYMINUTE=0)
Thanks in advance.
You want it to fire every month, so FREQ=MONTHLY;INTERVAL=1. Then it should be on the 28th day of the month, so BYMONTHDAY=28. Hours and minutes don't really matter so just put BYHOUR=8;BYMINUTE=0 or whatever you prefer.
There is a very simple way to test this. APEX automation use the oracle scheduling calendar syntax, which can be evaluated using dbms_scheduler.evaluate_calendar_string
DECLARE
start_date TIMESTAMP;
return_date_after TIMESTAMP;
next_run_date TIMESTAMP;
l_interval VARCHAR2(500) := 'FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=28;BYHOUR=8;BYMINUTE=0';
BEGIN
start_date := current_timestamp;
return_date_after := start_date;
FOR i IN 1..5
LOOP
dbms_scheduler.evaluate_calendar_string
(
l_interval,
start_date,
return_date_after,
next_run_date
);
dbms_output.put_line('next_run_date: ' || TO_CHAR(next_run_date,'fmDay, DD-MON-YYYY HH24:MI SS'));
return_date_after := next_run_date;
END LOOP;
END;
/
next_run_date: Sunday, 28-NOV-2021 8:0 59
next_run_date: Tuesday, 28-DEC-2021 8:0 59
next_run_date: Friday, 28-JAN-2022 8:0 59
next_run_date: Monday, 28-FEB-2022 8:0 59
next_run_date: Monday, 28-MAR-2022 8:0 59

cannot get year-month interval

I am learning PL-SQL and the exercise is to find the year-month interval difference between two dates. I wrote the following code:
DECLARE
t1 TIMESTAMP (2) WITH TIME ZONE := to_timestamp_tz('2019-01-21 21:05:53.46 +02:00',
'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM');
t3 TIMESTAMP WITH TIME ZONE := to_timestamp_tz('2020-01-21 21:05:53.46 +02:00',
'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM');
ym INTERVAL YEAR(2) to MONTH;
BEGIN
-- ym := '10-2';
ym := t3-t1;
DBMS_OUTPUT.PUT_LINE(ym);
END;
I would expect the ym variable to give '01-0' (1 year difference), but I get an error:
Error report -
ORA-06550: line 9, column 9:
PLS-00382: expression is of wrong type
ORA-06550: line 9, column 3:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I am really confused why this is happening, I tried changing the precision of the YEAR(_), but that doesn't help.
If ym is of datatype INTERVAL DAY(2) to SECOND(2), I get correct result. If I replace ym to ym := '10-2'; it also works. But with ym INTERVAL YEAR(2) to MONTH it is not working :(
I found in the psoug.org examples that you could use this syntax:
ym := (t3-t1) year to month;
dbfiddle demo
t3-t1 does not work as the resulting value is of the INTERVAL DAY TO SECOND data type and there is no implicit cast to INTERVAL YEAR TO MONTH.
Instead, you can use NUMTOYMINTERVAL( MONTHS_BETWEEN( t3, t1 ), 'MONTH' ); to dynamically create the correct data type:
DECLARE
t1 TIMESTAMP (2) WITH TIME ZONE := to_timestamp_tz('2019-01-21 21:05:53.46 +02:00',
'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM');
t3 TIMESTAMP WITH TIME ZONE := to_timestamp_tz('2020-01-21 21:05:53.46 +02:00',
'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM');
ym INTERVAL YEAR(2) to MONTH;
BEGIN
ym := NUMTOYMINTERVAL( MONTHS_BETWEEN( t3, t1 ), 'MONTH' );
DBMS_OUTPUT.PUT_LINE(ym);
END;
/
outputs:
+01-00
db<>fiddle here

ORA-01873: the leading precision of the interval is too small with EXTRACT function

I faced strange issue with EXTRACT function when trying to get the seconds interval of 8-12 hours.
This is tested with Oracle 12.2
Example:
https://rextester.com/ZMR79428
declare
start_time_ TIMESTAMP;
exect_time_ NUMBER;
begin
start_time_ := SYSDATE- 1/3; -- 8 hours, error is for SYSDATE- 1/2
exect_time_ := extract(day from ((sysdate- start_time_)*86400));
dbms_output.put_line(exect_time_);
end;
I can't see what wrong I've done to get the leading precision of the interval is too small error in this code. It worked for all other scenarios.
Eg: SYSDATE -1/4 works fine.
If you subtract two DATE data type values you get a NUMBER representing the number of (fractional) days between the two values. If you subtract two TIMESTAMP data type values you get an INTERVAL data type.
So your answer could simply be:
declare
start_time_ DATE := SYSDATE- 1/3;
exect_time_ NUMBER;
begin
exect_time_ := ( sysdate - start_time_ ) *86400;
dbms_output.put_line( exect_time_ );
end;
/
Which outputs: 28800
Your problem is that you are multiplying by 86400 inside the EXTRACT function rather than outside; so SYSDATE - start_time_ gives INTERVAL '8' HOUR and then you are multiplying by 86400 and INTERVAL '8' HOUR * 86400 would give a value of INTERVAL '28800' DAY which does not fit into the default precision of an INTERVAL DAY TO SECOND data type (and would give you the wrong answer anyway).
What you would want (if you really want to use TIMESTAMPs) is:
declare
start_time_ TIMESTAMP := SYSTIMESTAMP - 1/3;
difference INTERVAL DAY TO SECOND := SYSTIMESTAMP - start_time_;
exect_time_ NUMBER;
begin
exect_time_ := EXTRACT( DAY FROM difference ) * 24 * 60 * 60
+ EXTRACT( HOUR FROM difference ) * 60 * 60
+ EXTRACT( MINUTE FROM difference ) * 60
+ EXTRACT( SECOND FROM difference );
dbms_output.put_line( exect_time_ );
end;
/
Which outputs something like 28800.246382 (as there is a fraction of a second between the two SYSTIMESTAMP calls).
or, if you do not care about fractional seconds then:
declare
start_time_ TIMESTAMP := SYSTIMESTAMP- 1/3;
exect_time_ NUMBER;
begin
exect_time_ := ( SYSDATE - CAST( start_time_ AS DATE ) ) * 86400;
dbms_output.put_line( exect_time_ );
end;
/
Which outputs 28800.
db<>fiddle
what I don't get is why it gives error
It is a strange error; the code below tests various cases:
DECLARE
TYPE test_case IS RECORD(
units VARCHAR2(20),
difference INTERVAL DAY TO SECOND,
multiplier NUMBER(8,0)
);
TYPE test_case_list IS TABLE OF test_case;
FUNCTION createTestCase(
units VARCHAR2,
difference INTERVAL DAY TO SECOND,
multiplier NUMBER
) RETURN test_case;
test_cases test_case_list := test_case_list(
createTestCase( 'SECOND', INTERVAL '1' SECOND, 24 * 60 * 60 ),
createTestCase( 'SECOND PLUS A LITTLE', INTERVAL '1' SECOND + INTERVAL '0.001' SECOND, 24 * 60 * 60 ),
createTestCase( 'MINUTE', INTERVAL '1' MINUTE, 24 * 60 ),
createTestCase( 'MINUTE PLUS A LITTLE', INTERVAL '1' MINUTE + INTERVAL '0.001' SECOND, 24 * 60 ),
createTestCase( 'HOUR', INTERVAL '1' HOUR, 24 ),
createTestCase( 'HOUR PLUS A LITTLE', INTERVAL '1' HOUR + INTERVAL '0.001' SECOND, 24 ),
createTestCase( 'DAY', INTERVAL '1' DAY, 1 ),
createTestCase( 'DAY PLUS A LITTLE', INTERVAL '1' DAY + INTERVAL '0.001' SECOND, 1 )
);
FUNCTION createTestCase(
units VARCHAR2,
difference INTERVAL DAY TO SECOND,
multiplier NUMBER
) RETURN test_case
IS
tc test_case;
BEGIN
tc.units := units;
tc.difference := difference;
tc.multiplier := multiplier;
RETURN tc;
END;
BEGIN
FOR i IN 1 .. test_cases.COUNT LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE( test_cases(i).units );
DBMS_OUTPUT.PUT_LINE( test_cases(i).difference * test_cases(i).multiplier * 100000 );
DBMS_OUTPUT.PUT_LINE( (SYSTIMESTAMP + test_cases(i).difference - SYSTIMESTAMP) * test_cases(i).multiplier * 10000 );
DBMS_OUTPUT.PUT_LINE( (SYSTIMESTAMP + test_cases(i).difference - SYSTIMESTAMP) * test_cases(i).multiplier * 100000 );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE( SQLERRM );
END;
END LOOP;
END;
/
and outputs:
SECOND
+000100000 00:00:00.000000000
+000009999 21:21:36.000000000
ORA-01873: the leading precision of the interval is too small
SECOND PLUS A LITTLE
+000100100 00:00:00.000000000
+000010009 22:33:36.000000000
+000100099 19:12:00.000000000
MINUTE
+000100000 00:00:00.000000000
+000009999 23:59:31.200000000
ORA-01873: the leading precision of the interval is too small
MINUTE PLUS A LITTLE
+000100001 16:00:00.000000000
+000010000 03:59:02.400000000
+000100001 15:55:12.000000000
HOUR
+000100000 00:00:00.000000000
+000009999 23:59:59.280000000
ORA-01873: the leading precision of the interval is too small
HOUR PLUS A LITTLE
+000100000 00:40:00.000000000
+000010000 00:03:59.040000000
+000100000 00:39:55.200000000
DAY
+000100000 00:00:00.000000000
+000009999 23:59:59.970000000
ORA-01873: the leading precision of the interval is too small
DAY PLUS A LITTLE
+000100000 00:01:40.000000000
+000010000 00:00:09.960000000
+000100000 00:01:39.800000000
db<>fiddle here
Using INTERVAL directly in each test case works.
When you force the PL/SQL engine to work out SYSTIMESTAMP + an_interval - SYSTIMESTAMP then it must call the SYSTIMESTAMP function twice (which means that there is a few fractions of a second difference between the values) and the test cases fail.
When you add a tiny amount of time to the interval then the test cases all pass again suggesting that an Interval with a different precision has resulted from the calculation. (This is not intended to be a hacky solution, just an interesting side-note).
db<>fiddle shows that it only occurs in the PL/SQL scope; if you run the same statements in SQL statements then there are no exceptions.
There is probably a bug but it would require diving into the precision of the data-types returned when dynamically generating intervals from SYSTIMESTAMP to work out how exactly it is occurring; and, beyond being able to make a bug report to Oracle (which they might fix in a later version), it isn't going make your solution any more viable.
However, that's tangential to the solution; don't multiply your INTERVAL by 86400; you should be using EXTRACT multiple times with DAY, HOUR, MINUTE and SECOND respective arguments and converting the returned values of each to seconds and adding them or, alternatively, using CAST to convert back to use DATE arithmetic.
I think the issue is because of the limitation of the too-large numbers in intervals.
Actually, the Issue is with the multiplication.
If you execute it without or with a small multiplier than you will get an answer.
SQL> select extract(day from ((systimestamp - (systimestamp - 1/3)))) from dual ;
EXTRACT(DAYFROM((SYSTIMESTAMP-(SYSTIMESTAMP-1/3))))
---------------------------------------------------
0
SQL> select extract(day from ((systimestamp - (systimestamp - 1/3))*8640)) from dual ;
EXTRACT(DAYFROM((SYSTIMESTAMP-(SYSTIMESTAMP-1/3))*8640))
--------------------------------------------------------
2880
SQL> select extract(day from ((systimestamp - (systimestamp - 1/3))*86400)) from dual ;
select extract(day from ((systimestamp - (systimestamp - 1/3))*86400)) from dual
*
ERROR at line 1:
ORA-01873: the leading precision of the interval is too small
SQL>
Whatever you are doing with this number, you need to think of different logic as multiplication on the timestamp column is not recommended.
Cheers!!

Oracle Date Comparison Issue Totally confused - what am I doing wrong?

I am newbie & learning Oracle. I want to compare dates in function, while implementing the function I got an issue that output is wrong and not getting a valid answer I search a lot for a solution but in vain....
Solution so far I found was:
Do not compare Datetime to date.
If using DateTime, first TRUNC is needed to remove time factor.
While comparing date first convert dates to To_char(anyDate,'dd-mon-yyyy');
if we are comparing date like 1-dec-13 to 27-dec-13, make sure that both of the year are same. like both should be of 2013.
I have passed through all the constraints so far i found but fail to find the solution. Can any body help me. what blunder am i doing? Thanks in advance for anticipation.
SET serveroutput on;
DECLARE
startDate DATE := to_date('1'|| '/' ||to_char(sysdate, 'MM') || '/2013','DD/MM/YYYY');
secDate DATE :=to_date(SYSDATE, 'DD/MM/YYYY ');
BEGIN
DBMS_OUTPUT.PUT_LINE( 'if secDate '|| secDate ||' is Greater Then');
DBMS_OUTPUT.PUT_LINE( 'StartDate '|| startDate || ' Output = ' );
if(secDate>startDate)
then
DBMS_OUTPUT.PUT_LINE('True' );
else
DBMS_OUTPUT.PUT_LINE('false');
end if;
end;
Your points in order:
i An Oracle DATE holds date and time, accurate to the second. You can compare them, for example 12/27/2013 00:00:00 is less than 12/27/2013 09:00:00.
ii If you want to see if two DATE values with a time component (for example 12/27/2013 14:00:00 and 12/27/2013 12:34:56) are on the same day, then yes, you want TRUNC: IF TRUNC(firstDate) = TRUNC(secondDate).
iii There's nothing to be gained by using TO_CHAR for comparing DATE values. Use TRUNC if you don't want to include the time in the comparison; leave it out if you want the time.
iv The years don't have to be the same. A date like 12/27/2013 is greater than 12/27/2012 and less than 12/27/2014. If you leave it as a DATE type Oracle will do the right thing.
As for what's going wrong with your code, take a look at your startDate initialization:
'1'|| '-' ||to_char(sysdate, 'MM') || '2013'
For today, that will give you 1-122013, which doesn't match the format string DD/MM/YYYY in the TO_DATE call. That's why you're not getting a decent date.
If you want to get the first day of the current month, Oracle provides a convenient shortcut:
startDate := TRUNC(SYSDATE, 'MONTH');
So I'd forget about all the formatting and go with something like this; note how it's a lot simpler:
SET serveroutput on;
DECLARE
startDate DATE := TRUNC(SYSDATE, 'MONTH');
secDate DATE := SYSDATE;
BEGIN
DBMS_OUTPUT.PUT_LINE( 'if secDate '|| secDate ||' is Greater Then');
DBMS_OUTPUT.PUT_LINE( 'StartDate '|| startDate || ' Output = ' );
if(secDate>startDate) then
DBMS_OUTPUT.PUT_LINE('True' );
else
DBMS_OUTPUT.PUT_LINE('false');
end if;
END;
Note that secDate will be equal to startDate only at midnight on the first day of the month, when both values (for December 2013) will be 12/27/2013 00:00:00. At all other times of the month, secDate will be greater.
And note that I used 12/27/2013 00:00:00 to explain December 27, 2013 at midnight. That's just so I could explain the date in English. It's not how Oracle stores dates. How does Oracle store dates? It doesn't really matter - the important thing is that Oracle handles the date just fine if you leave it in DATE format and use TRUNC when you care only about the date and not about the time.
to_date(SYSDATE, 'DD/MM/YYYY ') that is your problem. It gives 27.12.0013 as a result. That is why you get false printed. Actually you don't need to convert sysdate to date as it is already of type date. Use this instead:
secDate DATE := SYSDATE;
See this line to_date('1'|| '-' ||to_char(sysdate, 'MM') || '2013','DD/MM/YYYY');
'1'|| '-' ||to_char(sysdate, 'MM') || '2013' gives you '1-122013', so you make
to_date('1-122013','DD/MM/YYYY');
The format don't match, you have to change one of them, either
to_date('01/'|| to_char(sysdate, 'MM')|| '/2013','DD/MM/YYYY');
or
to_date('01'|| '-' ||to_char(sysdate, 'MM')|| '2013','DD-MMYYYY')
Instead of secDate DATE :=to_date(SYSDATE, 'DD/MM/YYYY '); simply use secDate DATE :=TRUNC(SYSDATE);, there is no need for a conversion.

In Oracle, how can I detect the date on which daylight savings time begins / ends?

Is there a way in Oracle to select the date on which daylight savings will switch over for my locale?
Something vaguely equivalent to this would be nice:
SELECT CHANGEOVER_DATE
FROM SOME_SYSTEM_TABLE
WHERE DATE_TYPE = 'DAYLIGHT_SAVINGS_CHANGEOVER'
AND TO_CHAR(CHANGEOVER_DATE,'YYYY') = TO_CHAR(SYSDATE,'YYYY'); -- in the current year
Edit: I was hoping for a solution that would not require changes when Congress adjusts DST laws, as they did in 2007. The posted solutions will work, though.
To improve on Leigh Riffel's answer, this is much simpler with the same logic:
Function DaylightSavingTimeStart (p_Date IN Date)
Return Date Is
Begin
Return NEXT_DAY(TO_DATE(to_char(p_Date,'YYYY') || '/03/01 02:00 AM', 'YYYY/MM/DD HH:MI AM') - 1, 'SUN') + 7;
End;
Function DaylightSavingTimeEnd (p_Date IN Date)
Return Date Is
Begin
Return NEXT_DAY(TO_DATE(to_char(p_Date,'YYYY') || '/11/01 02:00 AM', 'YYYY/MM/DD HH:MI AM') - 1, 'SUN');
End;
We use the following two functions to calculate the start and end dates for any given year (post 2007, US).
Function DaylightSavingTimeStart (p_Date IN Date)
Return Date Is
v_Date Date;
v_LoopIndex Integer;
Begin
--Set the date to the 8th day of March which will effectively skip the first Sunday.
v_Date := to_date('03/08/' || to_char(p_Date,'YYYY') || '02:00:00 AM','MM/DD/YYYY HH:MI:SS PM');
--Advance to the second Sunday.
FOR v_LoopIndex IN 0..6 LOOP
If (RTRIM(to_char(v_Date + v_LoopIndex,'DAY')) = 'SUNDAY') Then
Return v_Date + v_LoopIndex;
End If;
END LOOP;
End;
Function DaylightSavingTimeEnd (p_Date IN Date)
Return Date Is
v_Date Date;
v_LoopIndex Integer;
Begin
--Set Date to the first of November this year
v_Date := to_date('11/01/' || to_char(p_Date,'YYYY') || '02:00:00 AM','MM/DD/YYYY HH:MI:SS PM');
--Advance to the first Sunday
FOR v_LoopIndex IN 0..6 LOOP
If (RTRIM(to_char(v_Date + v_LoopIndex,'DAY')) = 'SUNDAY') Then
Return v_Date + v_LoopIndex;
End If;
END LOOP;
End;
There is probably a simpler way to do it, but these have worked for us. Of course this query doesn't know whether daylight saving time is observed for where you are. For that you will need location data.
Instead of looping to get the next sunday you can also use the next_day(date, 'SUN') function of oracle.
In the United States, Daylight Savings Time is defined as beginning on the second Sunday in March, and ending on the first Sunday in November, for the areas that observe DST, for years after 2007.
I don't think there's an easy way to get this information from Oracle, but based on the standard definition, you should be able to write a stored procedure that calculates the beginning and ending date using the Doomsday Algorithm.
Here is a way to use Oracles internal knowledge of whether a timezone observes daylight saving time or not to determine the start and end of it. Aside from the complexity and general strangeness of it, it requires two timezones to be know have identical times when daylight saving time is not in effect and different times when it is. As such it is resilient to congressional changes in when daylight saving time occurs (assuming your database is up to date with the patches), but is not resilient to regional changes effecting the timezones keyed off of. With those warnings, here is what I have.
ALTER SESSION SET time_zone='America/Phoenix';
DROP TABLE TimeDifferences;
CREATE TABLE TimeDifferences(LocalTimeZone TIMESTAMP(0) WITH LOCAL TIME ZONE);
INSERT INTO TimeDifferences
(
SELECT to_date('01/01/' || to_char(sysdate-365,'YYYY') || '12:00:00','MM/DD/YYYYHH24:MI:SS')+rownum-1
FROM dual CONNECT BY rownum<=365
);
COMMIT;
ALTER SESSION SET time_zone='America/Edmonton';
SELECT LocalTimeZone-1 DaylightSavingTimeStartAndEnd
FROM
(
SELECT LocalTimeZone,
to_char(LocalTimeZone,'HH24') Hour1,
LEAD(to_char(LocalTimeZone,'HH24')) OVER (ORDER BY LocalTimeZone) Hour2
FROM TimeDifferences
)
WHERE Hour1 <> Hour2;
I told you it was strange. The code only figures out the day of the change, but could be enhanced to show the hour. Currently it returns 09-MAR-08 and 02-NOV-08. It is also sensitive to the time of year it is run, which is why I had to do the -365...+365. All in all I don't recommend this solution, but it was fun to investigate. Maybe someone else has something better.
Here's my version of the above. It's advantage is that it does not need a second 'alter session set time zone', and can be used more easily from an application.
You create the stored function, and then you simply use:
ALTER SESSION SET time_zone='Asia/Jerusalem';
select GetDSTDates(2012,1) DSTStart,GetDSTDates(2012,2) DSTEnd,SessionTimeZone TZ from dual;
which will return the dst start date,dst end date, timezone for the specified year.
create or replace function GetDSTDates
(
year integer,
GetFrom integer
)
return Date
as
cursor c is
select 12-to_number(to_char(LocalTimeZone at time zone '+00:00','HH24')) offset,
min(to_char(LocalTimeZone at time zone '+00:00','DD/MM/YYYY')) fromdate,
max(to_char(LocalTimeZone at time zone '+00:00','DD/MM/YYYY')) todate
from (
SELECT cast((to_date('01/01/'||to_char(year)||'12:00:00','MM/DD/YYYYHH24:MI:SS')+rownum-1) as timestamp with local time zone) LocalTimeZone
FROM dual CONNECT BY rownum<=365
)
group by 12-to_number(to_char(LocalTimeZone at time zone '+00:00','HH24'));
dstoffset integer;
offset integer;
dstfrom date;
dstto date;
begin
offset := 999;
dstoffset := -999;
for rec in c
loop
if rec.offset<offset
then
offset := rec.offset;
end if;
if rec.offset>dstoffset
then
dstoffset := rec.offset;
dstfrom := to_date(rec.fromdate,'DD/MM/YYYY');
dstto :=to_date(rec.todate,'DD/MM/YYYY');
end if;
end loop;
if (offset<999 and dstoffset>-999 and offset<>dstoffset)
then
if GetFrom=1
then
return dstfrom;
else
return dstto;
end if;
else
return null;
end if;
end;
/
ALTER SESSION SET time_zone='Asia/Jerusalem';
select GetDSTDates(2012,1) DSTStart,
GetDSTDates(2012,2) DSTEnd,
SessionTimeZone TZ from dual;
Old question but here's a new answer. Use 08-MAR for the first date since that skips the first week
--Start of DST
select next_day(to_date('08-MAR-' || to_char(sysdate, 'YYYY')), 'SUN') from dual
--End of DST
select next_day(to_date('01-NOV-' || to_char(sysdate, 'YYYY')), 'SUN') from dual

Resources