Help in changing plsql case when to decode

Hi,
Can anyone help me to change this sql for it to use decode function instead of case when? Below is the sql code
Thanks in advance.
SELECT
parts,
weeks,
SUM(qty) qty
FROM (
SELECT
CASE
WHEN ((is_tbd = 'yes' AND is_tbd_order = 'no') OR ex_fac_date > v_asofdate + 131) THEN 'tbd'
WHEN ex_fac_date BETWEEN v_asofdate - 1 AND v_asofdate + 5 AND is_tbd = 'no' THEN 'wk1'
WHEN ex_fac_date BETWEEN v_asofdate + 6 AND v_asofdate + 12 AND is_tbd = 'no' THEN 'wk2'
WHEN ex_fac_date BETWEEN v_asofdate + 13 AND v_asofdate + 19 AND is_tbd = 'no' THEN 'wk3'
WHEN ex_fac_date BETWEEN v_asofdate + 20 AND v_asofdate + 26 AND is_tbd = 'no' THEN 'wk4'
WHEN (ex_fac_date < v_asofdate - 1) AND is_tbd = 'no' THEN 'past_due'
END weeks,
ffdate,
parts,
SUM(qty) qty
FROM
delivery)

I can't use case because my oracle is 8i,Please join 21st Century
Can you help me checnge this to if then elsif insteadSQL does does contain IF statement!

Similar Messages

  • Help for changing the case of a selected word of a multiline text to upper

    DBVersion:10g
    Foms Version:10g
    Hi Experts,
    Actually i have a requirement where i have to change the case of a selected word by a user into upper case.
    ex: FFFFFF hhhhh GGGG
    when user selects hhhhh and click on some button or any other option it should change it to upper case.
    plss help me out.
    regards :(
    Edited by: user123 on May 11, 2011 12:46 PM

    Here is some PL/SQL code to change a particular occurence of a word in a text.
    In this case, I change the second occurence of the "two" word:
    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> set serveroutput on
    SQL>
    SQL> declare
      2   c varchar2(100) := 'one two three four one two three four one two three four' ;
      3   c2 varchar2(100);
      4   i integer;
      5   j integer := 0 ;
      6   pos integer := 1 ;
      7   occurence integer := 2 ; -- occurence to change
      8  begin
      9   loop
    10    i := instr(c,'two', pos);
    11    exit when j = (occurence-1) or i = 0;
    12    j := j + 1;
    13    pos := pos + i ;
    14   end loop;
    15   i := instr(c,'two', pos);
    16   if i > 0 then
    17     c2 := substr( c,1,i-1) || upper(substr(c,i, length('two')))
    18        || substr(c, i+length('two'));
    19   end if ;
    20   dbms_output.put_line(c2);
    21  end;
    22  /
    one two three four one TWO three four one two three four
    PL/SQL procedure successfully completed.Francois

  • Need help with changing letter cases : (

    Hey everyone, I have an assignment that needs to
    1) allow the user to enter a sentence
    2) count the number of upper case, lower case, spaces, and other characters in the sentence,
    3) convert each first letter of a word into upper case if not already in upper case,
    4) convert non first letters to lower case if not already in lower case
    So, I have been successfull counting all characters, but I can't seem to find a way to convert the letter cases.
    I understand I have to distinguish first letters and non first letters, and I did this by using a boolean.
    Also, I tried to use a for loop and convert the letter cases accordingly by += 32 and -= 32.
    This is what I have for now :
    import javax.swing.JOptionPane;
    public class Asg3 {
         public static void main(String [] args)     {
              String sentence = JOptionPane.showInputDialog("Enter a Sentence!");
              int length = sentence.length();
              int counter = 0;
              int uppercase = 0;
              int lowercase = 0;
              int space = 0;          
              int other = 0;
              int total = 0;
              boolean firstletter = true;
              char thischarac;
              for (counter = 0; counter < sentence.length(); counter++)
                        thischarac = sentence.charAt(counter);
                        if ( thischarac >= 65 && thischarac <= 90 ) {
                             ++ uppercase;
                   else if ( thischarac >= 97 && thischarac <= 122 ) {
                             ++ lowercase;
                   else if ( thischarac == 32) {
                             ++ space;
                   else {
                             ++ other;
                             ++ total;
              for (counter = 0; counter < sentence.length(); counter++) {
                   thischarac = sentence.charAt(counter);
                   if (firstletter == true) {
                             if (thischarac >= 97 && thischarac <= 122) {
                                  thischarac -= 32;
                   else
                        if (firstletter == false)
                             if (thischarac >= 65 && thischarac <= 90) {
                                  thischarac += 32;
    System.out.println("You have entered: " + sentence);
              System.out.println("Upper case Letters: " + uppercase);
              System.out.println("Lower case Letters: " + lowercase);
              System.out.println("The number of blank spaces: " + space);
              System.out.println("The number of other characters: " + other);
              System.out.println("Grand total: " + total);
              System.exit( 0 );               
    I am stuck on this for hours now, and all googling, asking friends, going over lectures have failed, and this is my last resort.
    I understand it is frustrating when students come here for homework, but I would really appreciate any hints or help.
    Thank you!

    Oh, sorry I'm new and didn't realize the code button. Anyways, here it is
    import javax.swing.JOptionPane;
    public class Asg3 {
         public static void main(String [] args)     {
              String sentence = JOptionPane.showInputDialog("Enter a Sentence!");
              int length = sentence.length();
              int counter = 0;
              int uppercase = 0;
              int lowercase = 0;
              int space = 0;          
              int other = 0;
              int total = 0;
              boolean firstletter = true;
              char thischarac;
              for (counter = 0; counter < sentence.length(); counter++)
                        thischarac = sentence.charAt(counter);
                        if ( thischarac >= 65 && thischarac <= 90 ) {
                             ++ uppercase;
                   else if ( thischarac >= 97 && thischarac <= 122 ) {
                             ++ lowercase;
                   else if ( thischarac == 32) {
                             ++ space;
                   else {
                             ++ other;
                             ++ total;
              for (counter = 0; counter < sentence.length(); counter++) {
                   thischarac = sentence.charAt(counter);
                   if (firstletter == true) {
                             if (thischarac >= 97 && thischarac <= 122) {
                                  thischarac -= 32;
                   else
                        if (firstletter == false)
                             if (thischarac >= 65 && thischarac <= 90) {
                                  thischarac += 32;
                                    System.out.println("You have entered: " + sentence);
              System.out.println("Upper case Letters: " + uppercase);
              System.out.println("Lower case Letters: " + lowercase);
              System.out.println("The number of blank spaces: " + space);
              System.out.println("The number of other characters: " + other);
              System.out.println("Grand total: " + total);
              System.exit( 0 );               
    }The problem I'm having is converting letters to their right cases. I need to change the first letters of a word to upper cases, and non first letters to lower cases. For example, if the sentence the user entered is
    "tHIS AssigNMent IS harD!" , I would have to convert it to : "This Assignment Is Hard!"
    What I tried to do is use a for loop and a boolean.
    If firstletter == true, and if the character was lower case, I would subtract 32 from that character.
    If firstletter == false, and if the character was upper case, I would add 32.
    The problem here is that, what do I have to do to make it print the converted sentence? I have tried System.out.println( "The new sentence is :" + sentence); which gives me only the original sentence,
    and System.out.println("The new sentence is :" + thiscarac); which gives me all capital letters.
    So there are two main problems:
    1) Is my forloop and boolean correct in converting the cases? Am I using the right method?
    2) If so, how do I get the system to printout the new converted sentence?
    Edited by: ShaRpy on Oct 28, 2008 4:51 PM

  • CASE WHEN and DECODE

    Hi everybody,
    I have column in Discoverer like this one shown bellow:
    DECODE(Vrstapolise,1,'Aktivno',4,'Kapitalisano',3,DECODE(Stanjepreisteka,'Aktivno','Aktivno','Kapitalisano','Kapitalisano',NULL),NULL)
    I want to make the same column with same functionality in OBI BMM. Is it possible? If it is possible, how to do that?
    Thanks in advance.

    As Sai said use Case stmt, your code will something like this
    Case Vrstapolise
    When 1 then 'Aktivno'
    When 4 then 'Kapitalisano'
    When 3 then
    Case Stanjepreisteka
    when 'Aktivno' then 'Aktivno'
    when 'Kapitalisano' then 'Kapitalisano'
    else Null end
    else Null End
    Thanks,
    Vino

  • Please Help: Trouble with nested CASE statement and comparing dates

    Please tell me why the query below is always returning the bold null even when the start_date of OLD is greater than or equal to the start_date of NEW.
    What I want to do is get the difference of the start_dates of two statuses ( Start_date of OLD - Start_date of NEW) if
    1. end_date of NEW is not null
    2. start_date of OLD is greater than start_date of NEW
    else return null.
    select id,
    case when max(end_date) keep (dense_rank last order by decode(request_wflow_status,'New',1,0),start_date) is null then
    null
    else
              case when max(decode(status,'OLD',start_date,null)) > max(decode(status,'NEW',start_date,null))
              then max(decode(status,'OLD',start_date,null)) - max(decode(status,'NEW',start_date,null))
    else
    null
    end
    end result
    from cc_request_status where id =1
    group by id;

    Avinash,
    Thank you for your help.. Here is a more description of my problem..
    Here is a sample of data I have for a table with four columns (id,status,start_date,end_date)
    What I need to do is to get difference of the start dates of the maximum available dates, if data is valid. The pseducode is as follows:
    IF end_date of New status is null
    THEN return null
    ELSE
    IF start_date of old >= start_date of new
    THEN return (start_date of old - start_date of new)
    ELSE return null
    I used the following query but always return the bold null
    select id,
    (case when max(end_date) keep (dense_rank last order by decode(status,'new',1,0),start_date) is null then
    null
    else
              (case when max(decode(status,'old',start_date,null)) >=
              max(decode(status,'new',start_date,null))
              then max(decode(status,'old',start_date,null)) - max(decode(status,'new',start_date,null))
    else
    null
    end)
    end) result
    from tbl where id =1
    Based on the below sample, I expected to get the following result; 14-Mar-07 - 16-Feb-07 which is the difference of the maximum start_dates of the two statuses. However the query is not working.. Please help me.. Thank you..
    Id    Status    start_date      end_date
    1     new      03-Feb-07      07-Feb-07
    1     new      16-Feb-07      21-Feb-07
    1     old      '10-Mar-07      12-Mar-07
    1     old      '14-Mar-07      16-Mar-07

  • Case when statement not working

    hi there, I am trying to work out how to get my case statement to work.
    I have got the following code. 
    select pthproto.pthdbo.cnarole.tpkcnarole, pthproto.pthdbo.cnaidta.formataddr as formataddr, cnaidta.dateeffect as maxdate, isnull(cast (pthproto.pthdbo.cnaaddr.prefix1key as varchar (50)),'') + ' ' + isnull(cast (pthproto.pthdbo.cnaaddr.prefix2key
    as varchar (50)),'')+ ' ' + isnull(cast (pthproto.pthdbo.cnaaddr.prefix3key as varchar (50)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.houseidkey as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component1
    as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component2 as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component3 as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component4
    as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component5 as varchar (100)),'') as mailaddress, row_number() over(partition by pthproto.pthdbo.cnarole.tpkcnarole order by cnaidta.dateeffect desc) as rn into #address from pthproto.pthdbo.cnarole
    inner join pthproto.pthdbo.cnaidty on cnarole.tfkcnaidty =cnaidty.tpkcnaidty inner join pthproto.pthdbo.cnaidta on cnaidty.tpkcnaidty = cnaidta.tfkcnaidty inner join pthproto.pthdbo.cnaaddr on cnaidta.tfkcnaaddr = cnaaddr.tpkcnaaddr order by cnaidta.dateeffect
    select *, case when mailaddress is not null then mailaddress else formataddr end as test from #address where tpkcnarole = '18306695'
    The case when statement is struggling with how i have created the column mailaddress.  As it does seem to understand when it is null.  In the example I have got there is no value in any of the columns to create
    the mailaddress.  Hence why I am referencing it from elsewhere.  Due to having a way on the system where it picks up data from 2 different places.    The mailaddress is always correct if there is one, hence why
    trying to reference that one first.  So how do i change this case when statement to work ?            

    It's ok I have fixed my own problem
    when
    (mailaddress
    is
    null 
    or mailaddress

    then formataddr
    else mailaddress
    end
    as test
    case

  • 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

  • SQL - Select Help - Case When? Return Value from Second Table?

    Hi - next to folks on this board I am probably somewhere between a Beginner and an Intermediate SQL user.
    Ive been using a case when statement in plsql to find "all those who's status in any program was cancelled during a specific time, but have become or are still active in a second program"
    So, Im effectively trying to return a value from a second table in a case when, but its not liking anthing other than a declared text like 'Yes' or 'No'.
    Here is the select statement - is there another way to do this where I can get the results I need?
    case when pp.party_id in (select pp1.party_id  -- Cancelled clients Active in another program
                                       from asa.program_participation          pp1,
                                            asa.curr_prog_participation_status cpps1
                                      where pp1.program_participation_id = cpps1.program_participation_id
                                        and pp1.party_id = pp.party_id
                                        and cpps1.code_value = 'ACT')
                then 'Yes' else 'No' end  as Active_in_Other_Prg
    So - in place of 'Yes' i basically want the program that they are active in or pp1.program_id, else Null
    It is possible that the client can be active in more than one program as well.
    Any assistance is greatly appreciated, i explored with if's and decodes but I cant get anything to work.
    Batesey

    Sounds like an outer join. See ora doc: Joins
    select p.*
    ,      q.party_id
    ,      q.program_id
    from   table_with_party_id p
    ,    ( select pp1.party_id  -- Cancelled clients Active in another program
           ,      pp1.program_id
           from   asa.program_participation          pp1,
                  asa.curr_prog_participation_status cpps1
           where  pp1.program_participation_id = cpps1.program_participation_id
           and    pp1.party_id = pp.party_id
           and    cpps1.code_value = 'ACT') q
    where p.party_id = q.party_id ( +)
    Note: In the example above there shoudn't be a space between the ( and +), but the forum software automagically converts this to
    The outer join will link show all records from the p table and only records from q if the party_id matches, ie q.party_id and q.program_id  will be null if there is no match.
    edit: added program_id

  • How to change a case to decode

    how will u convert this case statement into a decode statement
    please help
    case when to_char(sysdate,'DD-MON-YYYY') - onhand.t_date< 30 then SUM (moh.transaction_quantity) end as '30days',
    case when to_char(sysdate,'DD-MON-YYYY') - onhand.t_date >= 30 and to_char(sysdate,'DD-MON-YYYY') - onhand.t_date < 60 then SUM (moh.transaction_quantity) end as '30 TO 60 DAYS',
    case when to_char(sysdate,'DD-MON-YYYY') - onhand.t_date >= 60 and to_char(sysdate,'DD-MON-YYYY') - onhand.t_date < 90 then SUM (moh.transaction_quantity) end as '60 TO 90 DAYS' ,
    case when to_char(sysdate,'DD-MON-YYYY') - onhand.t_date >= 90 and to_char(sysdate,'DD-MON-YYYY') - onhand.t_date < 180 then SUM (moh.transaction_quantity) end as '90 TO 180 DAYS',
    case when to_char(sysdate,'DD-MON-YYYY') - onhand.t_date >= 180 then SUM (moh.transaction_quantity) end as '180 DAYS'

    I agree, the TO_CHAR(SYSDATE) expressions in the original query make no sense.
    Before CASE came along you had to DECODE(SIGN(...)), e.g:
    WITH test AS
         ( SELECT DATE '2007-02-20' AS dt FROM dual UNION ALL
           SELECT DATE '2007-02-04' FROM dual UNION ALL
           SELECT DATE '2007-02-01' FROM dual UNION ALL
           SELECT DATE '2006-12-01' FROM dual UNION ALL
           SELECT DATE '2006-01-01' FROM dual )
    SELECT dt
         , SIGN((SYSDATE -30) - dt)
         , DECODE(SIGN((SYSDATE -30) - dt),
                  -1, 'Y')
    FROM   test;
    DT          SIGN((SYSDATE-30)-DT) D
    20-FEB-2007                    -1 Y
    04-FEB-2007                    -1 Y
    01-FEB-2007                    -1 Y
    01-DEC-2006                     1
    01-JAN-2006                     1There was a DECODE(1, SIGN(...)) variant that was sometimes useful as well. Great days...

  • My Ipad2 just shuts down and will not turn on. Rebooting does not help. He switched on when I start charging it. In this case, the battery is charged.

    My Ipad2 just shuts down and will not turn on. Rebooting does not help. He switched on when I start charging it. In this case, the battery is charged.

    Standard troubleshooting steps in the following order:
    1. Reset: Press the sleep/wake button & home button at the same time, keep pressing until you see the apple logo, then release. Ignore the slide to power off.
    If no change, continue:
    2. Restore your phone from a backup, note: if the problem is related to corrupt data, while this will restore a fresh OS, it will also restore the problem.
    If no change, continue:
    3. Restore as a "new" phone in itunes & re-sync your content. All saved data will be lost.
    If no change, make an appointment at an Apple store.

  • My number 5 key is not working and my password has a number 5 in it. How can I change my password when I'm signed in on another account or as guest user? I need help :(

    My number 5 key is not working and my password has a number 5 in it. How can I change my password when I'm signed in on another account or as guest user? I need help

    - If the "other" account has administrative privileges then just try changing the PW for your account.
    - Forgot Mac Password? How to Reset Your Mac Password (with or without CD)
    Change the Admin Password with Mac OS X Single User Mode
    Reset mac mini admin password: Apple Support Communities
    - If the problem is due to a bad KB just get a new KB

  • HELP! Downloaded LR5 and when open it shows an error message saying error while trying to change from modules and I can not even import photos. Am I doing something wrong?

    HELP! Downloaded LR5 and when open it shows an error message saying error while trying to change from modules and I can not even import photos. Am I doing something wrong?

    I have the same problem, and the solutions in   Error changing modules | Lightroom do not work.  Even uninstalling and reinstalling a new copy did not solve the problem.  As anyone got an idea what other lingering files might be causing the problem?

  • CASE WHEN statement in DECODE

    SELECT v_startdate, v_enddate,
    (CASE WHEN SYSDATE BETWEEN v_startdate AND v_enddate THEN
    ‘active’
    ELSE
    ‘inactive’
    END) status
    FROM correction_tab;
    Could you kindly guide us as to how can we use DECODE and get the desired output.
    Thanks,
    Rami Reddy.

    You can, like this.
    However, the CASE seems a lot clearer to me so why bother with a DECODE?
    sql> with correction_tab as
      2    ( select trunc(sysdate)-1 as v_startdate, trunc(sysdate)   as v_enddate from dual
      3    union all
      4      select trunc(sysdate)   as v_startdate, trunc(sysdate)+1 as v_enddate from dual
      5    union all
      6      select trunc(sysdate)+1 as v_startdate, trunc(sysdate)+2 as v_enddate from dual
      7    )
      8  SELECT v_startdate
      9  ,      v_enddate
    10  ,      CASE
    11           WHEN SYSDATE BETWEEN v_startdate AND v_enddate
    12             THEN 'active'
    13           ELSE 'inactive'
    14         END status
    15  ,      decode ( sign(sysdate-v_startdate), 1, decode(sign(v_enddate-sysdate), 1, 'active', 'inactive'), 'inactive')
    as status2
    16  FROM correction_tab
    17  /
    V_STARTDA V_ENDDATE STATUS   STATUS2
    12-DEC-12 13-DEC-12 inactive inactive
    13-DEC-12 14-DEC-12 active   active
    14-DEC-12 15-DEC-12 inactive inactive

  • Using case when statement or decode stament in where clause

    hi gems..
    i have a problem in the following query..
    i am trying to use case when statement in the where clause of a select query.
    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
    cr.salary as salary
    from customer_details cr
    where (case when '>' = '>' then 'cr.salary > 5000'
    when '>' = '<' then 'cr.salary < 5000'
    when '>' = '=' then 'cr.salary = 5000'
    else null
    end);
    the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
    thats why for running the query, i have put '>' in that place.
    so the original query will look like this(for your reference):
    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
    cr.salary as salary
    from customer_details cr
    where (case when variable = '>' then 'cr.salary > 5000'
    when variable = '<' then 'cr.salary < 5000'
    when variable = '=' then 'cr.salary = 5000'
    else null
    end);
    so, in actual case,if the user selects '>' then the filter will be "where cr.salary > 5000"
    if the user selects '<' then the filter will be "where cr.salary < 5000"
    if the user selects '=' then the filter will be "where cr.salary = 5000"
    but i am getting the error "ORA 00920:invalid relational operator"
    please help..thanks in advance..

    Hi,
    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
           cr.salary                                      as salary
    from customer_details cr
    where (    v_variable = 'bigger'
           and cr.salary > 5000
       or (    v_variable = 'less'
          and cr.salary < 5000
       or (    v_variable = 'eq'
            and cr.salary = 5000
           )Edited by: user6806750 on 22.12.2011 14:56
    For some reason I can't write in sql '<', '>', '='

  • Security question keep saying they are wrong and I know they are right. I can't change them cuz when I try it tells me to answer the questions. I have tried everything changing my account and all and still can't get it can some one please help me out.

    Security question keep saying they are wrong and I know they are right. I can't change them cuz when I try it tells me to answer the questions. I have tried everything changing my account and all and still can't get it can some one please help me out.

    Go here and select "Reset your password."  You will receive an email message with a link that bypasses the questions.  You will then be able to select and answer the questions again.

Maybe you are looking for