Skip to Main Content
  • Questions
  • give range of ISO weeks allowed for a year.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, RUCHI.

Asked: September 11, 2018 - 6:05 am UTC

Last updated: September 13, 2018 - 1:06 am UTC

Version: 12c forms builder

Viewed 1000+ times

You Asked

Hi,

I have to include startweek and endweek validation in an oracle form. Here, startweek and endweek includes year and a week number like '201801'.
Whenever user enters a startweek or endweek value beyond the allowed range then I have to throw an error.
Which means for a given year that user enters, I have to find the range of startweek and endweek.
Also, there is no custom table that has year data beyond 2019.
I have just joined an IT company as a fresher. Please, provide an answer that can help me solve the issue.

Here, I am trying to write a code that gives weeks in a given year

SET SERVEROUTPUT ON;
declare
L_YEAR number;
L_DATE date;
FIRST_DAY date;
LAST_DAY date;
week char(10);
begin
L_YEAR:=2019; ------this value will be taken from user via an oracle form
L_DATE:=TO_DATE('01-JAN-'||L_YEAR,'DD-MM-YYYY');
select TRUNC (L_DATE, 'YEAR') into FIRST_DAY from DUAL;----first day of a year-----
select ADD_MONTHS(TRUNC (L_DATE ,'YEAR'),12)-1 into last_day from DUAL;-------last day of an year -----
DBMS_OUTPUT.PUT_LINE(FIRST_DAY||' '||LAST_DAY);
select to_char(last_day,'iw') into WEEK from DUAL;------this gives min week(i.e startweek information. I hoped it to give endweek information....
DBMS_OUTPUT.PUT_LINE(week);
end;

and Connor said...

Thanks for the code.

I'm not sure why you need to validate the *minimum* week - it is always 01 no?

For last week, we can:

- take the 2019
- add 1 to it (2020)
- convert that to a date (10-SEP-2020)
- truncate it to the year (01-JAN-2020)
- subtract 1 (31-DEC-2019)
- get the week for that date

Now given the way that IW works, is that might be the case that the last day of the year is seen as week 1 for then next year. So we can decrement that date until the week is not 01

SQL> set serverout on
SQL> declare
  2    l_year int := 2019;
  3    l_date date;
  4  begin
  5    l_date := trunc(to_date(to_char(l_year+1),'YYYY'),'YYYY')-1;
  6    while to_char(l_date,'IW') = '01'
  7    loop
  8      l_date := l_date - 1;
  9    end loop;
 10    dbms_output.put_line(l_date||': Week'||to_char(l_date,'IW'));
 11  end;
 12  /
29-DEC-19: Week52

PL/SQL procedure successfully completed.


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

No need for looping

Anton, September 12, 2018 - 9:50 am UTC

There's no need for looping I think. 4 January is always in ISO-week 01, so 7 days before that it's always the last week of the previous year. leading to
to_char( trunc( to_date( l_year+1, 'YYYY' ), 'Y' ) - 4, 'IW' )
Connor McDonald
September 13, 2018 - 1:06 am UTC

Good point.

 set serverout on
 declare
      l_year int := 2020;
      l_date date;
    begin
      l_date := trunc(to_date(to_char(l_year+1),'YYYY'),'YYYY')-1;
      while to_char(l_date,'IW') = '01'
      loop
        l_date := l_date - 7;
      end loop;
     dbms_output.put_line(l_date||': Week'||to_char(l_date,'IW'));
   end;
   /


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library