CASE/DECODE in WHERE caluse

Hi friends,
Let's consider an EMP table has the following structure and data:
create table EMP
EMPNO NUMBER(4) not null,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2),
CONFDATE DATE
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800, null, 20, to_date('27-12-1980', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600, 300, 30, to_date('28-02-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7521, 'WARD', 'SALESMAN', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250, 500, 30, to_date('28-02-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7566, 'JONES', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975, null, 20, to_date('12-04-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7654, 'MARTIN', 'SALESMAN', 7698, null, 1250, 1400, 30, to_date('28-09-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850, null, 30, to_date('11-05-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7782, 'CLARK', 'MANAGER', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450, null, 10, to_date('19-06-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7788, 'SCOTT', 'ANALYST', 7566, null, 3000, null, 20, to_date('09-12-1982', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000, null, 10, to_date('27-11-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500, 0, 30, to_date('18-09-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7876, 'ADAMS', 'CLERK', 7788, null, 1100, null, 20, to_date('12-01-1983', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7900, 'JAMES', 'CLERK', 7698, null, 950, null, 30, to_date('03-12-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000, null, 20, to_date('13-12-1981', 'dd-mm-yyyy'));
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, CONFDATE)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-03-1981', 'dd-mm-yyyy'), 1300, null, 10, to_date('31-05-1981', 'dd-mm-yyyy'));
commit;
Now I need to fetch the employees whose hire date is between 01-APR-1981 and 31-DEC-1981. If the hire date is null, then confirmation date has to be considered.
For this input the following employees's records should be returned:
7566
7654
7698
7782
7839
7844
7900
7902
I tried with CASE, DECODE in WHERE clause. But I couldn't succeed. Please help.
Thanks in advance.
Iniyavan

SQL> ed
Wrote file afiedt.buf
  1* select * from emp_test where nvl(hiredate,confdate)  between '01-APR-1981' and '31-DEC-1981'
SQL> /
EMP_TESTNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO CONFDATE
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20 12-APR-81
      7654 MARTIN     SALESMAN        7698                 1250       1400         30 28-SEP-81
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30 11-MAY-81
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10 19-JUN-81
      7839 KING       PRESIDENT            17-NOV-81       5000                    10 27-NOV-81
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30 18-SEP-81
      7900 JAMES      CLERK           7698                  950                    30 03-DEC-81
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20 13-DEC-81
8 rows selected.@boopathi: Why to_char is needed here ? Further to_char(hire date,'DD_MON_YY') and comparing with '01-APR-1981' ? and WHAT is "_" doing in date format ? You sholud test your query before posting.
Edited by: Saubhik on May 17, 2010 11:33 PM

Similar Messages

  • No output for XML Publisher Report using CASE/DECODE in Where Clause

    Hi,
    I've a business requirement to modify an existing report which has two input parameters,
    -> p_statcode (Closed Status) which can have values 'Y' or 'N'
    -> p_overdue (Overdue Flag) which can have values 'Y' or 'N'
    The Overdue Flag is an evaluated column having values of Y/N and it is evaluated as follows,
    ONTF_MOD_VAL(NVL (
                                         (TRUNC (SYSDATE)
                                          - (TO_DATE (oe_order_lines.attribute18,
                                                      'DD-MON-RRRR')
                                             + TO_NUMBER (fnd_lookup_values.meaning))),
                                         0
                            overdue_flagThe user requirement now is they needs to be a third option for parameter p_overdue called ALL,
    passing which the output should include records having
    p_statcode is Y ELSE p_statcode is N AND p_overdue is Y OR p_overdue is N
    In other words records having both Y and N vlaues for Overdue Flag have to be returned irrespective of the value given to Closed Status.
    Original where clause in the Data Definition file is as follows,
    WHERE Closed_Status = nvl(:p_statcode,Closed_Status)
                       AND overdue_flag = nvl(:p_overdue,overdue_flag)My modified code is as follows,
    WHERE   Closed_Status = NVL (:p_statcode, Closed_Status)
             AND overdue_flag = (CASE
             WHEN :p_overdue = 'Y' THEN 'Y'
             WHEN :p_overdue = 'N' THEN 'N'
             ELSE overdue_flag
             END)
    OR
    WHERE   Closed_Status = NVL (:p_statcode, Closed_Status)
             AND overdue_flag = DECODE (:p_overdue, 'Y', 'Y', 'N', 'N',overdue_flag)Both approaches have the same problem.
    The output is in EXCEL format. The modified query works fine for p_overdue as Y or N but when p_overdue is passed as ALL it returns an empty EXCEL sheet with just the report output column headers.
    Any help as to why this is the case ?? What is wrong in my approach ?
    Regards,
    Vishal

    not clear about p_overdue = ALL
    which values needed for p_overdue = ALL ?
    try smth like
    WHERE   Closed_Status = NVL (:p_statcode, Closed_Status)
    AND (
       overdue_flag = DECODE (:p_overdue, 'Y', 'Y', 'N', 'N',overdue_flag) and :p_overdue != 'ALL'
       or
      :p_overdue = 'ALL' and (overdue_flag = 'Y' or overdue_flag = 'N')
    )for overdue_flag which has more then 'Y', 'N' values
    if overdue_flag only in ('Y','N') then
    WHERE   Closed_Status = NVL (:p_statcode, Closed_Status)
    AND (
       overdue_flag = DECODE (:p_overdue, 'Y', 'Y', 'N', 'N',overdue_flag) and :p_overdue != 'ALL'
       or
      :p_overdue = 'ALL'
    )

  • Case statement in where clause ??

    Hello gurus,
    Can we use case statements in where clause ?? Any example will be great!
    And also i would like to know, besides CASE and DECODE statements, Is there any way we can use IF ELSE statements in SELECT clause or in WHERE clause ?
    Thank you!!

    Hi,
    user642297 wrote:
    Hoek,
    Thanks for the reply
    Whatever you return from 'then' should match your criteria.I didnt get this part...can you elaborate this part ?? Thank you!!Remember what a CASE expression does: it returns a single value in one of the SQL data types (or NULL).
    You're probably familiar with conditions such as
    WHERE   col = 1Inthe example above, col could be replaced by any kind of expression: a function call, and operation (such as "d * 24") or a CASE expression, which is exactly what Hoek posted:
    where  case
             when col = 6 then 1
             when col = 9 then 1
           end = 1;I think what Hoek meant about mnatching was this: since the CASE expression is being compared to a NUMBER, then every THEN clause (as well as the ELSE, if there is one) should return the same data type. You can't have one THEN clause return a NUMBER, and another one in the same CASE expression return a DATE, like this:
    where  case
             when col = 6 then 1
             when col = 9 then SYSDATE     -- WRONG! Raises ORA-00932: inconsistent datatypes
           end = 1; 
    By the way, it's rare when a CASE expression really helps in a WHERE clause. CASE is great for doing conitional stuff in places where you otherwise can't (in the ORDER BY clause, for example), but the WHERE clause was designed for conditions.
    Hoek was just trying to give a simple example. If you really wanted those results, it would be simpler to say:
    where  col = 6
    or     col = 9and simpler still to say
    where  col  IN (6, 9)

  • Case, Decode or PlSQL

    Hello,
    I am new to Oracle. I have a, hopefully basic SQL question.
    There is sometime four users in my dbs. But, I would like make query where the users below are returned, even if not in db.
    1. Bob
    2. Jane
    3. John
    4. Abul
    I have rows in db:
    ROWS IN DB
    Username account status
    bob Support Active
    Jane Dev Active
    John Dev Expired
    How I can write query to find all users, if one is missing. Try to get format
    Bob's Support account is active
    Jane's Dev account is active
    John's Dev account is active
    Abul is not in db
    I try CASE and Decode, but get bad returns.
    Pls help.
    Tring to learn SQL. Go to class in three weeks.

    Hi,
    Welcome to the forum!
    So you have four special users, each with a name and id number. That sounds like data. Data belongs in tables. You should create a table that has four rows. Then you could use that table to do an outer-join with other tables, such as db.
    If you don't have such a table, you can generate a result set on the fly that will serve as a table. The sub-query special_users below is just that:
    WITH  special_users  AS
         SELECT  'Abul' AS username  FROM dual  UNION ALL
         SELECT     'BOB'                 FROM dual  UNION ALL
         SELECT     'Jane'                 FROM dual  UNION ALL
         SELECT     'John'                 FROM dual
    SELECT     su.username
        ||  CASE
              WHEN  db.username IS NULL
              THEN  ' is not in db'
              ELSE  '''s ' || account
                              || ' account is '
                        || status
             END     AS msg
    FROM     special_users     su
    LEFT OUTER JOIN          db     ON     db.username     = su.username
    ;If you do have a real special_users table, then the main query above works;l just skip the WHERE clause (the forst 7 lines) and, the query is:
    SELECT     su.username
        ||  CASE
              WHEN  db.username IS NULL
              THEN  ' is not in db'
              ELSE  '''s ' || account
                              || ' account is '
                        || status
             END     AS msg
    FROM     special_users     su
    LEFT OUTER JOIN          db     ON     db.username     = su.username
    ;Edited by: Frank Kulash on Sep 28, 2009 3:53 PM
    The subject line you chose for this message, "Case, Decode or PlSQL" is very good, and shows the order in which you should try things.
    (1) CASE is the general way to handle if-then-else logic in a SQL expression
    (2) DECODE (and other specialized features, such as NVL2, which Michaels used) can save you a little typing in special circumstances. Once you have mastered CASE, you can start exploring these other expressions.
    (3) PL/SQL is the last resort, used only if there is no good way to do something in SQL.

  • Case/Decode

    Hello All,
    my table1 below
    FILE_ID    
    1
    2
    3Table2
    FILE_ID, FILE_NAME, P_FILE_ID
    11          pdp01             1
    12          pdp02              1
    13          pdp03              1I am trying something like this
    select    case WHEN d.file_name LIKE '%01%' THEN d.file_id
                  end 01_file_id_value,
            case WHEN d.file_name LIKE '%02%' THEN d.file_id
                  end 02_file_id_value
    from TABLE1 r , TABLE2 d
    where d.p_file_id = r.file_id'I am receiving something like
    01_file_id_value        02_file_id_value
    11                           
                                       12I need something like
    01_file_id_value        02_file_id_value
    11                              12I am not sure if we can do this with case/decode please help/guide me

    Hi,
    It looks like you want an aggregate query, something like this:
    select    MIN ( case
                         WHEN  d.file_name LIKE '%01%'  THEN d.file_id
                   end
               )        AS "01_file_id_value",                  -- non-standard alias must be in double-quotes
              MIN ( case
                        WHEN  d.file_name LIKE '%02%'  THEN d.file_id
                   end
               )        AS "02_file_id_value"                  -- non-standard alias must be in double-quotes
    from        TABLE1  r
    ,        TABLE2  d
    where        d.p_file_id = r.file_id
    ;What results would you want if there were two (or more) non-NULL values in the same column?
    Post a little sample data (CREATE TABLE and INSERT statements) for all tables, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.

  • CASE Statement in Where Condition with Multi Valued parameter in SSRS

    Hi All,
    I am little confused while using CASE statement in Where condition in SSRS. Below is my scenario:
    SELECT
    Logic here
    WHERE
    Date IN (@Date)AND
    (CASE
    WHEN NAME LIKE 'ABC%' THEN 'GROUP1'
    WHEN ID IN ('123456', '823423','74233784') THEN 'GROUP2'
    WHEN ABC_ID IS NULL THEN 'GROUP3'
    ELSE 'GROUP4'
    END ) IN (@GROUP)
    So above query uses WHERE condition with CASE statement from @GROUP parameter. I want to pass this parameter as multi- valued parameter and hence I have used CASE statement IN (@GROUP).
    For @Date one dataset will pass the available and default values and
    for @GROUP parameters, another dataset will pass the available and default values.
    But this is not working as expected. Please suggest me where I am making mistake in the query.
    Maruthu | http://sharepoint-works.blogspot.com

    Hi Maruthu,
    According to your description, I create a sample report in my local environment. It works as I expected. In your scenario, if the selected values from the Date parameter contains some of the Date field values, the selected values from the GROUP parameter
    contains some of GROUPS (‘GROUP1’,’GROUP2’,’GROUP3’,’GROUP4’) and the corresponding when statement is executed , then the dataset returns the corresponding values.
    In order to trouble shoot this issue, could you tell us what results are you get and what’s your desired results? If possible, you can post the sample data with sample dataset, then we can make further analysis and help you out.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • [svn:fx-trunk] 11933: Addressing an edge case for TileList where at times the listItems array doesn 't accurately reflect a fully realized set of item renderers.

    Revision: 11933
    Revision: 11933
    Author:   [email protected]
    Date:     2009-11-18 06:55:18 -0800 (Wed, 18 Nov 2009)
    Log Message:
    Addressing an edge case for TileList where at times the listItems array doesn't accurately reflect a fully realized set of item renderers.  This can occur when the TileList row/col creation routine bails out early when it believes it has no further need to create renderers due to current height of the list itself.
    QE notes:  None
    Doc notes: None
    Bugs: SDK-24169
    Reviewer: Alex
    Tests run: List, TileList, HorizontalList, DataGrid
    Is noteworthy for integration: No
    Ticket Links:
        http://bugs.adobe.com/jira/browse/SDK-24169
    Modified Paths:
        flex/sdk/trunk/frameworks/projects/framework/src/mx/controls/listClasses/ListBase.as

    Due to a copy/paste glitch, some necessary spaces have inadvertently been removed.  If I could fix this, I would.

  • How to use CASE stmt in Where clause

    Hi,
    How to use CASE stmt in WHERE clause?. I need the code. Please send me as early as possible..........

    Hi,
    1004977 wrote:
    Hi,
    How to use CASE stmt in WHERE clause?. There's no difference between how a CASE expression is used in a WHERE clause, and how it is used in any other clause. CASE ... END always returns a single scalar value, and it can be used almost anywere a single scalar expression (such as a column, a literal, a single-row function, a + operation, ...) can be used.
    CASE expressions aren't needed in WHERE clauses very much. The reason CASE expressions are so useful is that they allow you to do IF-THEN-ELSE logic anywhere in SQL. The WHERE clause already allows you to do IF-THEN-ELSE logic, usually in a more convenient way.
    I need the code.So do the peple who want t help you. Post a query where you'd like to use a CASE expression in the WHERE clause. Post CREATE TABLE and INSERT statements for any tables used unless they are commonly available, such as scott.emp). Also, post the results you want from that sample data, and explain how you get those resuts from that data.
    See the forum FAQ {message:id=9360002}
    Please send me as early as possible..........As mentioned bfore, that's rude. It's also self-defeating. Nobody will refuse to help you because you don't appear pushy enough, but some people will refuse to help you if you appear too pushy.

  • Case satement in where Clause..

    Hi I haev issue facing in procedure there are 4 param as in put some time 2 parameter will have value and some time it is null and and one parameter has multiple value . and this value i am passing to cursor and cursor should return value even though value is null so using case satement in where clasue but it is throwing error.
    here syntax
    SELECT strategicinitiativeid, projectid, -- ltm, ltmdeptid,
    YEAR,
    SUM (netexpensebudget) netexpensebudget,
    SUM (devsoftcapbudget + purchasesoftcapbudget
    + hardwarecapbudget
    ) AS totalcapbudget,
    SUM (netexpensebudgetinprog) netexpensebudgetinprog,
    SUM ( devsoftcapbudgetinprog
    + purchasesoftcapbudgetinprog
    + hardwarecapbudgetinprog
    ) AS totalcapinprogbudget
    FROM vpendingprojectsaligntosi
    WHERE CASE
    WHEN v_projectid IS NOT NULL
    THEN projectid IN (v_projectid)
    END and case v_SIID is not null
    then strategicinitiativeid=v_SIID end
    AND budgetstepid IN (v_budgetstepid)
    GROUP BY strategicinitiativeid, YEAR, projectid;

    user13301356 wrote:
    Hi I haev issue facing in procedure there are 4 param as in put some time 2 parameter will have value and some time it is null and and one parameter has multiple value . and this value i am passing to cursor and cursor should return value even though value is null so using case satement in where clasue but it is throwing error.
    here syntax
    SELECT strategicinitiativeid, projectid, -- ltm, ltmdeptid,
    YEAR,
    SUM (netexpensebudget) netexpensebudget,
    SUM (devsoftcapbudget + purchasesoftcapbudget
    + hardwarecapbudget
    ) AS totalcapbudget,
    SUM (netexpensebudgetinprog) netexpensebudgetinprog,
    SUM ( devsoftcapbudgetinprog
    + purchasesoftcapbudgetinprog
    + hardwarecapbudgetinprog
    ) AS totalcapinprogbudget
    FROM vpendingprojectsaligntosi
    WHERE CASE
    WHEN v_projectid IS NOT NULL
    THEN projectid IN (v_projectid)
    END and case v_SIID is not null
    then strategicinitiativeid=v_SIID end
    AND budgetstepid IN (v_budgetstepid)
    GROUP BY strategicinitiativeid, YEAR, projectid;may be --untested
    SELECT strategicinitiativeid,
           projectid,
           YEAR,
           SUM(netexpensebudget) netexpensebudget,
           SUM(devsoftcapbudget + purchasesoftcapbudget + hardwarecapbudget) AS totalcapbudget,
           SUM(netexpensebudgetinprog) netexpensebudgetinprog,
           SUM(devsoftcapbudgetinprog + purchasesoftcapbudgetinprog +
               hardwarecapbudgetinprog) AS totalcapinprogbudget
      FROM vpendingprojectsaligntosi
    WHERE projectid in nvl(v_projectid, projectid)
       AND strategicinitiativeid in nvl(v_SIID, strategicinitiativeid)
       AND budgetstepid in (v_budgetstepid)
    GROUP BY strategicinitiativeid, YEAR, projectid

  • CASE in a WHERE clause : ORA-00905

    Hi everyone,
    I am trying to use a CASE in a WHERE clause and I got the error ORA-00905: missing keyword. Here is my code :
    SELECT id_reserv,
         concat(nom,concat('_',indice)) as nom,
         libelle,
         num_lot,
         resa_keyword1,
         resa_keyword2,
         resa_keyword3,
         resa_keyword4,
         date_creation,
         comm_creation,
         id_util,
         actif,
         id_env_act,
         (SELECT login from utilisateur u where u.id_util=r.id_util) AS nom_util,
         (SELECT nom from environnement e where e.id_env=r.id_env_act) AS nom_env,
         (SELECT count(*) from reserv_comp rc where rc.id_reserv=r.id_reserv) AS nbComp,
         (SELECT count(*) from reserv_modif mc where mc.id_reserv=r.id_reserv) AS nbModif
    FROM reservation r
         WHERE r.nom NOT LIKE 'RESERV_LOT_%'
         AND id_util='1'
         AND actif='1'
         AND id_env_act>='-1'
         AND CASE sansdemande
         WHEN true THEN id_reserv not in select id_reserv from demande_livraison where id_env_dep>='-1'
         ELSE id_reserv=id_reserv
         END
         AND nom LIKE '%'
    ORDER BY date_creation;
    I already looked at the CASE statement and it seems that the syntax is correct, so I'm not sure I can use it in a WHERE clause.
    Any help would be nice !
    Guich

    Hi,
    it should be something like this:
    AND CASE
      WHEN 'false'='true' THEN (select id_reserv from demande_livraison where id_env_dep>='-1')
      ELSE id_reserv
         END = id_reserv The subquery should give one row back
    But I think it is better to write your query as:
    SELECT id_reserv,
         concat(nom,concat('_',indice)) as nom,
         libelle,
         num_lot,
         resa_keyword1,
         resa_keyword2,
         resa_keyword3,
         resa_keyword4,
         date_creation,
         comm_creation,
         id_util,
         actif,
         id_env_act,
         (SELECT login from utilisateur u where u.id_util=r.id_util) AS nom_util,
         (SELECT nom from environnement e where e.id_env=r.id_env_act) AS nom_env,
         (SELECT count(*) from reserv_comp rc where rc.id_reserv=r.id_reserv) AS nbComp,
         (SELECT count(*) from reserv_modif mc where mc.id_reserv=r.id_reserv) AS nbModif
    FROM reservation r
         WHERE r.nom NOT LIKE 'RESERV_LOT_%'
         AND id_util='1'
         AND actif='1'
         AND id_env_act>='-1'
         AND
                          (  'false'='true' and id_reserv not in (select id_reserv from demande_livraison where id_env_dep>='-1')
                          or
                          ( 'true'= 'true' and id_reserv=id_reserv
         AND nom LIKE '%'
    ORDER BY date_creation;This coding in SQL something as if a=b then c, else d in the simplest form.
    Herald ten Dam
    http://htendam.wordpress.com

  • Case statement within where clause

    How can i write a case statement within Where clause of SQL statement.
    Ex:
    If sysdate is less than Dec 31 of 2009 then run the query for 2009 else run the query for 2010.
    belwo query is not working. Please let me know how can i write a case statement within where clause.
    Select * from table
    where
    Case
    when to_char(sysdate,'yyyymmdd')<=20091231 then tax_year=2009
    else tax_year=2010
    End

    Hi,
    You can get the results you want like this:
    Select  *
    from      table
    where   tax_year = Case
                      when  to_char(sysdate,'yyyymmdd') <= 20091231
                      then  2009
                      else  2010
                 End
    ;A CASE expression returns a single value in one of the SQL data types, such as a NUMBER, VARCHAR2 or DATE. There is no boolean type in SQL.

  • Can i use Decode in Where clause

    Hi,
    Can i use Decode in Where clause Please Do the need full on the same.
    Thanks,
    Sanjeev.

    set serveroutput on
    DECLARE
    posn  PLS_INTEGER := 0;
    empid PLS_INTEGER := 178;
    x     NUMBER;
    BEGIN
      SELECT NVL(SUM(ah.quantity * ah.saleprice * ap.payoutpct), 0)
      INTO x
      FROM accessoryhistory ah, payoutpercentage ap,
      sku s, store st
      WHERE empid = DECODE(posn,
                              0, st.areadir,
                              1, st.areamgr,
                              2, NVL(st.storemgr1, st.storemgr2),
                              3, NVL(st.asstmgr1, NVL(st.asstmgr2,
                           st.asstmgr3)))
      AND ah.statustype IN ('ACT', 'DEA')
      AND ah.store = st.store
      AND s.dbid = ah.dbid
      AND s.sku = ah.sku
      AND ap.productgroup = s.productgroup
      AND ap.position = posn;
      dbms_output.put_line(x);
    END;
    /http://psoug.org/reference/decode_case.html

  • Smart case Ipad 1; where is it?

    Smart case Ipad 1, where is it?

    Go back to the article you asked this question from, ignore the instructions for updating it wirelessly, scroll down, and follow those for updating it from iTunes on a computer.
    (78180)

  • How to use the CASE Expression in Where Cluase?

    Hi All,
    I'm trying to use the CASE Expression in the Where Clause at some trigger on the Form?
    I've tried this Code:
    Declare
    N Number;
    begin
    SELECT COUNT(E.EMP_SID)
         INTO N
         FROM EMPLOYEES E, RANKS R
         WHERE CASE WHEN R.qualification_sid = 1104 AND E.rank_sid = 8 THEN
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1)
         ELSE
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1)
         END
         BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR');
    END;
    When I run this code as a normal query at any SQL editor it works successfully, But When I Compile it at some trigger on the Form it gives me this error:
    Encountered the symbol "CASE" when expecting one of the following:
    ( - + mod ......
    Heeey how to specify the previous code to be shown as code in the thread?
    Note: I'm using Forms 6i

    OK I tried it and worked but for one condition:
    WHERE DECODE (E.qualification_sid, 1104,
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1),
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1))
         BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR')
    But how to put two conditions for the same Expression:
    WHERE DECODE ((E.qualification_sid, 1104) AND (E.RANK_SID, 8),
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1),
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1))
         BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR')
    The previous code gives me this error: missing right parenthesis

  • Decode in where clause

    Hi all,
    Is there any reason why decode cannot be assigned in the where clause?Shoudl we be using CASE instead ?Appreciate any suggestions.
    SELECT
    decode(abcd,'a','a,'b','b',NULL,'c','c')wxyz,
    from COMB_DATA
    where
    decode(abcd,'a','a,'b','b',NULL,'c','c')wxyz ='a';

    Do not specify aliases for expressions in conditions :-)))

Maybe you are looking for