Just a FYI regarding Case statements in ORacle 8i in pl/sql

Well..I saw numerous posts in this forum regarding not being able to do case statement within pl/sql. Well..you can do
that using dynamic SQL. IT works like a champ.

Hi,
Try first to (re)compile ll invalid objects. then
you can run:
select owner,type,count(*) from all_errors
group by owner,typeif you still have uncompiled objects, then report the different errors:
select * from all_errors...

Similar Messages

  • Case Statement in Oracle Query.

    Hello Oracle Gurus,
    I need suggestion on whether I should use Case statement in Oracle queries.
    I have a sql statement which inserts the data by selecting data from other table. While selecting the data I have put a logic in the select statement for one column which is something like this and there are some more similar statements in the same query.
    CASE
                   WHEN (b.ACCOUNT = 'FIN' or b.ACCOUNT ='FIN ACC' or b.ACCOUNT like '%Global Eq%' or b.ACCOUNT like '%Flexible Bond%')
                   THEN
                        'MTM'
                             WHEN (substr(a.CTC,-3)='MTM')
                   THEN
                        'MTM'
                             WHEN (substr(a.CTC,-3)='AFS' or substr(a.CTC,-3)='HTM' OR substr(a.CTC,-3)='ACC' OR substr(a.CTC,-3)='HFI' )
                   THEN
                        'ACC'
                   ELSE
                                  'OTH'
              END,
    I wanted to get an suggestion about how much performance issue can I have due to this in my insert statement.
    Let me know if you need any other information.All comments are really appreciated.

    Hi,
    It's depends on the joins how you have performed with source table, stats and existing of indexes. If the volume of data is high - in case of any performance issue - why can't you perform the required things of logical conditions of data and popualte in one cluster so - that you can fetch out the chunks of required information with better way and in support with indexes.
    Let us know the ful query your working on
    Oracle version
    Explain plans - get it from dbms_xplan.
    - Pavan Kumar N
    - ORACLE - 9i/10g - OCP
    RHCE - Enterprize Linux 5.4

  • CASE statement in Oracle 8i PL/SQL

    I'm in the process of doing an upgrade from 8i to 10g and have the necessary instructions in performing it. however, after running the pre upgrade tasks (run utlu102i.sql) to show the preupgrade information, i found out that there are some INVALID objects.
    Miscellaneous Warnings
    WARNING: --> Database contains INVALID objects prior to upgrade.
    .... USER INGITRN has 1 INVALID objects.
    .... USER INGIUAT has 3 INVALID objects.
    .... USER OEMMON has 7 INVALID objects.
    .... USER RE_ITF_USER has 11 INVALID objects.
    .... USER SYS has 1 INVALID objects.
    Would it be ok to proceed with the upgrade and ignore the warnings?
    Also, as i checked the invalid object for SYS, it shows:
    OWNER OBJECT_NAME OBJECT_TYPE STATUS
    SYS UTL_RECOMP PACKAGE VALID
    SYS UTL_RECOMP PACKAGE BODY INVALID
    Further more, i tried to do a fix by invoking utlirp.sql and the same objects are showing up. Same thing shows when invoking utlrp.sql or even doing a manual compile for the affected objects. I later found out that CASE statements are not accepted in PL/SQL for Oracle 8i.
    Can you help me fixed the issue on the invalid object?
    Thanks

    Hi,
    Try first to (re)compile ll invalid objects. then
    you can run:
    select owner,type,count(*) from all_errors
    group by owner,typeif you still have uncompiled objects, then report the different errors:
    select * from all_errors...

  • Regarding case statement and decode function

    Hi Experts,
    I have question.....regarding case statement and decode statement....
    Can you please explain me that which one will be efficient,to place in insert statement...
    insert statement(
    (case when ........then
                         case when ....then
                         else
                         end)
      else
    end)
    or
    insert statement(
    case when.....then
    decode(....)
    else
    end)
    Can you people explain me which one is more efficient method?
    Thanks in advance.......

    The are major differences to talk about in case of CASE vs DECODE, but performance wise both are pretty much the same.
    Have a look at Tom's thread
    Ask Tom "better performance - case or decode"
    I would suggest to use CASE whenever possible. Don't worry about the performance part.

  • Regarding case statement

    Hi guys,
    i am getting a very strange problem. i have score column. basis of this column i am sorting the dataand after it i am giving rank to it.
    but for one employee it is not giving rank. giving null.so please any suggestion.
    i am applying case statement for it.

    It can be because of case statement...Whats the purpose of case statement here ? plz elaborate..
    Try removing case and check if you are getting same error

  • Case statement in Oracle 8i

    can anyone tell me what to do to make the query below run in Oracle 8i.
    thanks
    select
    case connum
    when 'C251234' then 'Good'
    when 'C318260' then 'Ok'
    else connum end
    from names

    I believe the confusion arises since CASE and other new functions and features in 8i - analytic functions, NVL2 scalar subquery ..., did not work in PL/SQL in 8i due to it having a different SQL parsing engine. A common work around was to put the offending SQL in a view or execute it as dynamic SQL where PL/SQL would use the database parser.
    SQL> select
      2  case when connum = 'C251234' then 'Good'
      3  when connum = 'C318260' then 'Ok'
      4  else connum end
      5  from (
      6  select 'AAAA' connum from dual
      7  union all
      8  select 'C251234' from dual
      9  union all
    10  select 'C318260' from dual
    11  );
    CASEWHE
    AAAA
    Good
    Ok
    SQL> var c refcursor
    SQL> begin
      2  open :c for
      3  select
      4  case when connum = 'C251234' then 'Good'
      5  when connum = 'C318260' then 'Ok'
      6  else connum end
      7  from (
      8  select 'AAAA' connum from dual
      9  union all
    10  select 'C251234' from dual
    11  union all
    12  select 'C318260' from dual
    13  );
    14  end;
    15  /
    case when connum = 'C251234' then 'Good'
    ERROR at line 4:
    ORA-06550: line 4, column 1:
    PLS-00103: Encountered the symbol "CASE" when expecting one of the following:
    ( * - + all mod null <an identifier>
    <a double-quoted delimited-identifier> <a bind variable>
    table avg count current distinct max min prior sql stddev sum
    unique variance execute the forall time timestamp interval
    date <a string literal with character set specification>
    <a number> <a single-quoted SQL string>
    SQL> var c refcursor
    SQL> begin
      2  open :c for
      3  'select
      4  case when connum = ''C251234'' then ''Good''
      5  when connum = ''C318260'' then ''Ok''
      6  else connum end
      7  from (
      8  select ''AAAA'' connum from dual
      9  union all
    10  select ''C251234'' from dual
    11  union all
    12  select ''C318260'' from dual
    13  )';
    14  end;
    15  /
    PL/SQL procedure successfully completed.
    SQL> print c
    CASEWHE
    AAAA
    Good
    Ok
    SQL> select * from v$version where rownum = 1;
    BANNER
    Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production

  • Got error when use case statement in oracle stored procedure

    Hi,
    I have a query like:
    select merchant_id,
    case
    when product_type='K' then 'Production'
    when product_carrier='UC' THEN 'Shipping'
    end the_type
    from product_tbl
    where merchant_id=10114
    It works fine. But as soon as I put it into a stored procedure, I got error like:
    Encountered the symbol "CASE" when expecting one of the following:
    ( - + mod null <an identifier>
    Please help!

    Oracle 8i doesn't support CASE into PL/SQL, so as Kamal said, create a view with the CASE, and use this view into your PL/SQL.
    Nicolas.
    And an example here :
    Re: Execute Immediate doesnot work in 8i (8.1.7) and Ref Cursor not exec qu
    Sorry Kamal.
    Message was edited by:
    N. Gasparotto

  • Query regarding case statement

    Hi,
    I have a SP which has in_rc as an input parameter
    Now I have a select query which is as follows in pseudo
    select....
    where <condition1>
    And <condition2>
    And <condition3>
    Now my requirement is that I want to add one more filter based on the in_rc
    ie if in_rc is not null, then I want to add one more filter condition along with the existing condition else only the existing condition.
    pseudo:
    select....
    where <condition1>
    And <condition2>
    And <condition3>
    case <in_rc><when not null>then
    AND <condition 4>
    possible?
    regds,
    Sun

    I am not pedantic. Just pointing out some things to be aware of (should be allowed, shouldn't it?);)
    It only requires a few more NVL's. ;) or lnnvl? ;)
    michaels>  VAR mgr number
    michaels>  EXEC :mgr := 7782
    michaels>  SELECT ename, mgr
      FROM emp
    WHERE lnnvl (mgr != :mgr)
    ENAME             MGR
    KING                     /*  want nulls or not?  */
    MILLER           7782
    michaels>  EXEC :mgr := NULL
    michaels>  SELECT ename, mgr
      FROM emp
    WHERE lnnvl (mgr != :mgr)
    ENAME             MGR
    SMITH            7902
    ALLEN            7698
    WARD             7698
    JONES            7839
    MARTIN           7698
    BLAKE            7839
    CLARK            7839
    SCOTT            7566
    KING                
    TURNER           7698
    ADAMS            7788
    JAMES            7698
    FORD             7566
    MILLER           7782

  • Doubt Regarding Merge Statement in Oracle

    Hi,
    I have an SP which takes 3 parameters Lets say
    (in_empid, in_empname,in_age)
    here in_empid corresponds to the empid ie primary key for update/insert
    Now which of the approach will be better. Will there be problem in using Merge statements for updates/insert
    1. Approach 1
    Add one more flag in parameters in_action . Now if in_action = 'U' then write an update statement.If in_action='I' then write insert stmnt
    2. Approach 2
    write a merge stmnt as follows
    merge into employee e using
    ( select in_empid, in_empname,in_age from dual ) b
    on  ( b.in_empid = e.empid)
    WHEN MATCHED THEN
      UPDATE SET e.ENAME = in_empname,
                           e.age = in_age
    WHEN NOT MATCHED THEN
      INSERT
      VALUES (in_empid,in_empname,in_age) something like that
    Which would be preferred? I mean is there any restriction that merge can be used only to merge 2 tables?what are the drawbacks of using Merge?
    Regds,
    S

    Hi cd,
    Thanks for the reply.
    Actaully I was keeping the front-end code also in mind.
    If we click an update button, then they will have to manage a flag till the end to say that transaction was update. whereas when its an insert of new record, they have to maintain a falg till end to imply that the transaction was insert.
    I want to avoid this so that they need not maintain additional flag.
    Hence I was thinking of using MERGE statement.
    Will there be any problem in using merge for such scenarios?
    Regds,
    S

  • Help Regarding CASE Statement.

    hey !
    i m trying to apply COUNT to a column , with a particular condition.
    like.
    CASE
    WHEN( A > B)
    THEN COUNT(some column)
    else '0'
    end
    but its not working.
    can anyone tell me how to count only particular rows which satisfy the given condition in WHEN Clause.
    i m trying to add many columns from different tables by joining them and then adding column one by one satisfying different conditions.
    my final table looks like this.
    items in stock-----|-----items_not_in_STock----|----Items_fully_in_stock
    its something like this...!!!
    secondly , what if i dont want any ELSE condition.
    is it possible???

    I am not quite sure I understood your "requirement"...no tables, no sample data, no expected output...
    can anyone tell me how to count only particular rows which satisfy the given condition in WHEN ClauseAssuming your requirement is above i.e. getting count of resultset based on different conditions, following might help you.
    SQL> select sum(case when object_id > 10000 then 1 end) from t ;
    SUM(CASEWHENOBJECT_ID>10000THEN1END)
                                      74

  • Need help converting Excel IF statement into CASE WHEN statement for Oracle

    Hi,
    Hope someone can help I have tried various ways of getting this to work, to no avail!
    Bascially I have some figures that are minus figures, and I need to add them together to get a movement figure, but I need to treat the minus figures as minus, if that makes sense, rather than the usual... a minus and a minus makes a plus.
    For example:- Budget Figure = -1% and Actual Figure = -68% so the movement needs to be -69%.
    The IF statement I have been using in Excel is the following:-
    =IF(FO110<0,(FP110-(IF(FO110=0,1,FO110)*-1)),FP110-IF(FO110=0,1,FO110))
    Which when using the figures as above = -69%
    Cell FO = The Budget Figure
    Cell FP = The Actual Figure
    However, when I created the CASE statement in Oracle, the figure in the query comes back as -0.67, which is oviously not what I want to happen when both actual and budget are minus figures; however when they are a minus and a plus, it works perfectly fine.
    Any help on this would be most appreciated!
    Kind regards,
    Annmarie

    Happy I did'n mess something up :)
    Nevertheless, don't show it too much around because
    case when budget < 0
         then actual - case when budget = 0  /* will never happen */
                            then 1           /* will never happen */
                            else budget
                       end * (-1)            /* -budget * (-1) remains only */
         else actual - case when budget = 0
                            then 1
                            else budget
                       end
    endso at least try the following (if case is more readable as decode for you). Let's hope it works as I don't have a Database at hand
    case when budget < 0
         then actual + budget
         else actual - case when budget = 0
                            then 1
                            else budget
                       end
    end Regards
    Etbin

  • CASE statement falls straight  through to end

    Hello,
    I want the following code to execute a case statement when it hits todays day name. I know from the out put at the end that it is picking the name up but it wont act on the name in the case statement.
    This is my code,
    SQL> DECLARE num NUMBER;
      2  insert_rec VARCHAR2(150);
      3  party_id NUMBER;
      4  day_rec VARCHAR2(150);
      5  loop_max NUMBER;
      6  today VARCHAR2(20);
      7  yesterday VARCHAR2(20);
      8
      9  -- For debugging
    10  rec_count VARCHAR2(150);
    11
    12  BEGIN
    13    party_id := 3;
    14    loop_max := 7;
    15
    16    FOR loop_index IN 1 .. loop_max
    17    LOOP
    18
    19      EXECUTE IMMEDIATE 'SELECT MAX(SCH_ID) FROM FBT_SCHEDULE WHERE PAR_ID = ' || party_id || ''
    20      INTO num;
    21      -- For debugging
    22      -- rec_count := '... The last records party_id is ' || num;
    23      -- DBMS_OUTPUT.PUT_LINE(rec_count);
    24
    25      IF num IS NULL THEN
    26
    27        -- day_rec := sysdate;
    28        -- Find the start of the week for this period if the week starts on Sunday.
    29        -- For debugging
    30        -- DBMS_OUTPUT.PUT_LINE( 'select to_char(sysdate,'||''''||'DAY'||''''||') from dual');
    31        EXECUTE IMMEDIATE 'select to_char(sysdate,' || '''' || 'DAY' || '''' || ') from dual'
    32        INTO today;
    33        -- For debugging
    34        DBMS_OUTPUT.PUT_LINE(today);
    35
    36        day_rec :=
    37        CASE
    38        WHEN today = 'SUNDAY' THEN
    39          sysdate
    40        WHEN today = 'MONDAY' THEN
    41          sysdate -1
    42        WHEN today = 'TUESDAY' THEN
    43          sysdate -2
    44        WHEN today = 'WEDNESDAY' THEN
    45          sysdate -3
    46        WHEN today = 'THURSDAY' THEN
    47          sysdate -4
    48        WHEN today = 'FRIDAY' THEN
    49          sysdate -5
    50        WHEN today = 'SATURDAY' THEN
    51          sysdate -6
    52        END;
    53
    54        DBMS_OUTPUT.PUT_LINE('...The adjusted day is '||day_rec);
    55
    56        -- For debugging
    57        -- day_rec := '... The date is ' || day_rec;
    58        -- DBMS_OUTPUT.PUT_LINE(day_rec);
    59
    60        -- insert_rec := 'INSERT INTO FBT_SCHEDULE (PAR_ID, SCHEDULE_DATE) VALUES (''' || party_id || ''', TO_DATE(''' || day_rec || ''', ''' || 'dd / mon / rr' || '''' || '))';
    61        -- EXECUTE IMMEDIATE insert_rec;
    62
    63        ELSIF num > 0 THEN
    64
    65          EXECUTE IMMEDIATE 'SELECT SCHEDULE_DATE FROM FBT_SCHEDULE WHERE SCH_ID = ' || num || ''
    66          INTO day_rec;
    67
    68          day_rec := to_date(day_rec) + 1;
    69
    70          -- For debugging
    71          DBMS_OUTPUT.PUT_LINE(day_rec);
    72
    73          -- insert_rec := 'INSERT INTO FBT_SCHEDULE (PAR_ID, SCHEDULE_DATE) VALUES (''' || party_id || ''', TO_DATE(''' || day_rec || ''', ''' || 'dd / mon / rr' || '''' || '))';
    74          -- EXECUTE IMMEDIATE insert_rec;
    75
    76        END IF;
    77
    78      END LOOP;
    79    END;
    80  /
    THURSDAY
    ...The adjusted day is
    THURSDAY
    ...The adjusted day is
    THURSDAY
    ...The adjusted day is
    THURSDAY
    ...The adjusted day is
    THURSDAY
    ...The adjusted day is
    THURSDAY
    ...The adjusted day is
    THURSDAY
    ...The adjusted day is
    PL/SQL procedure successfully completed.
    SQL>Thank You for any help
    Ben

    Ok, i looked at this a little harder and have some advice for you.
    1) lose the dynamic SQL or use binds if you NEED to be dynamic.
        EXECUTE IMMEDIATE 'SELECT MAX(SCH_ID) FROM FBT_SCHEDULE WHERE PAR_ID = ' || party_id || ''
        INTO num;
    should just be
    SELECT MAX(SCH_ID) INTO num FROM FBT_SCHEDULE WHERE PAR_ID = party_id;
          EXECUTE IMMEDIATE 'select to_char(sysdate,' || '''' || 'DAY' || '''' || ') from dual'
          INTO today;
    should just be
    today := to_char(sysdate, 'DAY');I'm also having a hard time following the logic, you have a party_id that doesn't change, yet you query based on that repeatedly inside your loop. It should be outside the loop.
    In all likelyhood, you could do this in no procedural code, something to strive for if you have spare cycles.
    Oh yeah, if you absolutely MUST use dynamic sql (you posted example code and my non-dynamic approach doesn't apply) then you MUST use bind variables.
            EXECUTE IMMEDIATE 'SELECT SCHEDULE_DATE FROM FBT_SCHEDULE WHERE SCH_ID = ' || num || ''
            INTO day_rec;
    would be
    EXECUTE IMMEDIATE 'SELECT SCHEDULE_DATE FROM FBT_SCHEDULE WHERE SCH_ID = :my_bind_variable' INTO day_rec USING num;

  • Case statement problems in oracle forms 6i

    Hello,
    Any one can help me that how to use case statment in Oracle forms 6i.
    i have read one thread and there was no proper solution so could any one please let me know to use case statement.
    Please also let me know which category i should search for FORMS 6i.
    when i'm using below code with cursor then i'm getting error 103
    database:=11g
    application := forms 6i
    operating system:= win Xp
    code is given below :-
    cursor c1 is
    select nc.nomenclature_id,
    nvl(nc.category_value, 0) master,
    nvl(nc2.category_value, 0) case,
    nvl(nc3.category_value, 0) bundle,
    case
    when nvl(nc.category_value, 0) > 0 and
    nvl(nc2.category_value, 0) > 0 and
    nvl(nc3.category_value, 0) > 0 then
    'A' --All packouts Master, Case, Bundle
    when nvl(nc.category_value, 0) > 0 and
    nvl(nc2.category_value, 0) > 0 and
    nvl(nc3.category_value, 0) = 0 then
    'B' --Both Master and Case
    when nvl(nc2.category_value, 0) = 0 and
    nvl(nc3.category_value, 0) = 0 then
    'C' --Master Case Only
    else
    'N'
    end code
    from nomn_category nc, --master case
    (select nc2.nomenclature_id,
    nc2.category_value
    from nomn_category nc2
    where nc2.category_id = '230732') nc2,
    (select nc3.nomenclature_id,
    nc3.category_value
    from nomn_category nc3
    where nc3.category_id = '236566') nc3
    Edited by: Rahul on Feb 3, 2012 7:18 PM
    Edited by: Rahul on Feb 3, 2012 7:20 PM

    hello Andreas,
    It is ok but i dont have to use view there.
    i need to use without view, because this code has to be use in FORMS6i.
    But Forms6i doesn't support to case function. i'm newbie in Forms.
    If you can convert to below bold one portion(case) into decode then please help me or
    if you have any idea about Forms6i then please send me any link where is given explanation about excel report that how to make excel report through Forms6i step by step and that excel report should be generate on any dynamic path which is given by user:-
    cursor c1 is
    select nc.nomenclature_id,
    nvl(nc.category_value, 0) master,
    nvl(nc2.category_value, 0) case,
    nvl(nc3.category_value, 0) bundle,
    case
    when nvl(nc.category_value, 0) > 0 and
    nvl(nc2.category_value, 0) > 0 and
    nvl(nc3.category_value, 0) > 0 then
    *'A'*
    when nvl(nc.category_value, 0) > 0 and
    nvl(nc2.category_value, 0) > 0 and
    nvl(nc3.category_value, 0) = 0 then
    *'B' --Both Master and Case*
    when nvl(nc2.category_value, 0) = 0 and
    nvl(nc3.category_value, 0) = 0 then
    *'C'*
    else
    *'N'*
    end "code"
    from nomn_category nc,
    (select nc2.nomenclature_id,
    nc2.category_value
    from nomn_category nc2
    where nc2.category_id = '230732') nc2,
    (select nc3.nomenclature_id,
    nc3.category_value
    from nomn_category nc3
    where nc3.category_id = '236566') nc3
    thanks

  • [Oracle 8i] Handling null in case statement

    The following bit of my query is causing the error 'inconsistent datatypes':
    CASE
         WHEN b.days_diff < 0
         THEN plan_start
         ELSE plan_start + b.days_diff
    ENDThe problem is that b.days_diff can be null.
    I've tried to work around this, by using the NVL statement and by nesting 2 case statements to try to deal with the null, but can't seem to work around it.
    Does anyone have anything else I can try?

    Apologies...internet server went down yesterday...
    I've had this issue before with my database. When I look at user_tab_columns, it says plan_start is a date. However, I have had to use TO_DATE in the past with it to get some things to work. It's very odd, and I have no idea why it's like that.
    Anyway, below, I have provided some sample data, and the full query I'm trying to run:
    CREATE TABLE     ord (
    ord_nbr          char(10)
    ord_stat     char(2)
    INSERT INTO     ord
    VALUES          ('0000012345', 'OP');
    INSERT INTO     ord
    VALUES          ('0000012346', 'OP');
    INSERT INTO     ord
    VALUES          ('0000012347', 'CL');
    INSERT INTO     ord
    VALUES          ('0000012348', 'OP');
    CREATE TABLE     op (
    ord_nbr          char(10)
    operation     char(4)
    op_status     char(2)
    plan_start     date
    pln_due          date
    dt_complete     date
    INSERT INTO     op
    VALUES          ('0000012345', '0001', 'CL', {ts '2009-01-01 00:00:00'}, {ts '2009-01-02 00:00:00'}, {ts '2009-01-04 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012345', '0002', 'CL', {ts '2009-01-02 00:00:00'}, {ts '2009-01-03 00:00:00'}, {ts '2009-01-06 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012345', '0003', 'CL', {ts '2009-01-03 00:00:00'}, {ts '2009-01-04 00:00:00'}, {ts '2009-01-09 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012345', '0004', 'WK', {ts '2009-01-04 00:00:00'}, {ts '2009-01-05 00:00:00'}, {ts '1900-12-31 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012345', '0005', 'OP', {ts '2009-01-05 00:00:00'}, {ts '2009-01-06 00:00:00'}, {ts '2009-01-02 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012346', '0001', 'CL', {ts '2009-01-01 00:00:00'}, {ts '2009-01-02 00:00:00'}, {ts '2009-01-01 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012346', '0002', 'OP', {ts '2009-01-02 00:00:00'}, {ts '2009-01-04 00:00:00'}, {ts '1900-12-31 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012346', '0003', 'OP', {ts '2009-01-04 00:00:00'}, {ts '2009-01-06 00:00:00'}, {ts '1900-12-31 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012347', '0001', 'CL', {ts '2009-01-01 00:00:00'}, {ts '2009-01-02 00:00:00'}, {ts '2009-01-03 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012347', '0002', 'CL', {ts '2009-01-02 00:00:00'}, {ts '2009-01-05 00:00:00'}, {ts '2009-01-06 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012348', '0001', 'OP', {ts '2009-01-15 00:00:00'}, {ts '2009-01-17 00:00:00'}, {ts '1900-12-31 00:00:00'});
    INSERT INTO     op
    VALUES          ('0000012348', '0002', 'OP', {ts '2009-01-17 00:00:00'}, {ts '2009-01-20 00:00:00'}, {ts '1900-12-31 00:00:00'});Full query:
    SELECT     ord.ord_nbr
    ,     ord.ord_stat
    ,     op.operation
    ,     op.op_status
    ,     op.plan_start
    ,     //where I want to return plan_start + days_diff if days_diff is not null or negative
         //otherwise, I want to return just plan_start
         AS adjusted_start
    ,     op.dt_complete
    ,     b.max_cl_op
    ,     b.pln_due
    ,     b.dt_complete
    ,     b.days_diff
    FROM     ord
    ,     op
         SELECT     a.ord_nbr
         ,     a.max_cl_op
         ,     op.pln_due
         ,     op.dt_complete
         ,     (op.dt_complete-op.pln_due)     AS days_diff
         FROM     (
              SELECT          op.ord_nbr
              ,          MAX(TO_NUMBER(op.operation))     AS max_cl_op
              FROM          ord
              ,          op
              WHERE          ord.ord_nbr     = op.ord_nbr
              AND          ord.ord_stat     != 'CL'
              AND          op.op_status     ='CL'
              GROUP BY     op.ord_nbr
              ) a
         WHERE     op.ord_nbr     = a.ord_nbr
         AND     op.operation     = a.max_cl_op
         ) b
    WHERE     ord.ord_nbr     = op.ord_nbr
    AND     op.ord_nbr     = b.ord_nbr (+)
    AND     ord.ord_stat     != 'CL'
    AND     op.op_status     != 'CL'
    AND     op.plan_start     <= ADD_MONTHS(sysdate, 12)The results I want to get:
    ord.ord_nbr     ord.ord_stat     op.operation     op.op_status     op.plan_start     adjusted_start     op.dt_complete     b.max_cl_op     b.pln_due     b.dt_complete     b.days_diff
    '0000012345'     'OP'          '0004'          'WK'          1/4/2009     1/9/2009     12/31/1900     '0003'          1/4/2009     1/9/2009     5     
    '0000012345'     'OP'          '0005'          'OP'          1/5/2009     1/10/2009     12/31/1900     '0003'          1/4/2009     1/9/2009     5
    '0000012346'     'OP'          '0002'          'OP'          1/2/2009     1/2/2009     12/31/1900     '0001'          1/2/2009     1/1/2009     -1
    '0000012346'     'OP'          '0003'          'OP'          1/4/2009     1/4/2009     12/31/1900     '0001'          1/2/2009     1/1/2009     -1
    '0000012348'     'OP'          '0001'          'OP'          1/15/2009     1/15/2009     12/31/1900                                   
    '0000012348'     'OP'          '0002'          'OP'          1/17/2009     1/17/2009     12/31/1900     

  • Oracle CASE statement logic

    Hi all,
    I have to compare the value of a varchar variable using a CASE statement and display the corresponding output.
    But when the following code is being executed, and i gave the value of dayrange as anything other than number, i am getting the error;
    The daysrange variable can be a number or a string (Hence i declared it as a varcahr2)
    Error report:
    ORA-06502: PL/SQL: numeric or value error: character to number conversion error
    ORA-06512: at line 5
    06502. 00000 - "PL/SQL: numeric or value error%s"
    *Cause:   
    *Action:
    declare
    daysrange varchar2(10):='abc';
    x varchar2(100);
    begin
    CASE WHEN DAYSRANGE = 1 THEN x := 'LD';
              WHEN DAYSRANGE BETWEEN 2 AND 7 THEN x := 'LW';
              WHEN DAYSRANGE BETWEEN 8 AND 30 THEN x:= 'LM';
              WHEN DAYSRANGE BETWEEN 31 AND 90 THEN x:= 'L3M';
              WHEN DAYSRANGE BETWEEN 91 AND 180 THEN x:= 'L6M';
              WHEN DAYSRANGE BETWEEN 181 AND 365 THEN x:= 'LY';
              WHEN DAYSRANGE BETWEEN 366 AND 730 THEN x:= 'L2Y';
              WHEN DAYSRANGE > 730 THEN x:= 'O2Y';
         ELSE x:='x:= x';
         END case;--DATERANGE
    exception
    when case_not_found then
    x:='something';
    dbms_output.put_line(x);
    end;
    Edited by: Chaitanya on Nov 25, 2010 1:25 AM

    Hi,
    Chaitanya wrote:
    ... The daysrange variable can be a number or a string (Hence i declared it as a varcahr2)That's usually not a good design. It would be better to have two variables (or columns) if necessary, a VARCHAR2 and a NUMBER.
    If you can't change the design, then test daysrange, and then do different things depending on whether it is a number or not.
    For example:
    declare
         daysrange      varchar2(10)     := '17';
         daysrange_n     NUMBER;
         x           varchar2(100);
    begin
         IF  REGEXP_LIKE (daysrange, '^\d+$')
         THEN
              daysrange_n := TO_NUMBER (daysrange);
              x := CASE 
                   WHEN daysrange_n > 730     THEN 'O2Y'
                   WHEN daysrange_n > 365     THEN 'L2Y'
                   WHEN daysrange_n > 180     THEN 'L1Y'
                   WHEN daysrange_n >  90     THEN 'L6M'
                   WHEN daysrange_n >  30     THEN 'L3M'
                   WHEN daysrange_n >   7     THEN 'LM'
                   WHEN daysrange_n >   1     THEN 'LW'
                   WHEN daysrange_n =   1     THEN 'LD'
                                  ELSE  x          -- If necessary
                   END;
         END IF;
    ...The tests in a CASE expression are done in order. The n-th WHEN condition is tried only after conditions 1 through n have failed. That's why we can saY, for example,
    "daysrange_n > 365" instead of
    "daysrange_n BETWEEN 366 AND 730". If the 2nd test is even being performed, we know that the 1st test failed, and that daysrnage_n is not > 730.
    I'm not saying that you have to write CASE expressions like this, or that it's necessarily better. You should know that it's possible, then choose whichever way makes the most sense in this situation.

Maybe you are looking for