Using cfif in a where clause clause

Hi all

This question is somewhat related to a previous post of mine on the polling dates.  I'm trying to use a cfif statement in my where clause to determine if a field is empty or not.  Here is the code:

< cfquery name = "GetPastEvents" datasource = "DSN" >

SELECT *.

SITE: calendar

WHERE DatePart ("yyyy", [StartDate]) = < cfqueryparam value = ' #Dateformat (Today, 'yyyy') # "cfsqltype = 'CF_SQL_DATE' / >

and < cfif EndDate NEQ "> < cfelse > StartDate, EndDate < / cfif > < < cfqueryparam value =" "#Today #" cfsqltype = 'CF_SQL_DATE' / > "

and Archive = < cfqueryparam value = '0' cfsqltype = "CF_SQL_INTEGER" / >

ORDER BY StartDate ASC, StartTime ASC

< / cfquery >

My question that this CF tells me EndDate is not defined.  EndDate is a field in the SITE table: calendar.  There must be a way to make this work, no?  Thank you!

You mix the ColdFusion variables (required by cfif) and SQL column names. Think of it this way, all CF tags and function run before the SQL query is sent to SQL server. You are this mixture and try to run CF instruction on SQL Server and thus fail.

You need something like this:

SELECT *.

SITE: calendar

WHERE DatePart ("yyyy", [StartDate]) =

and)

(enddate is not null and EndDate <> )

or (enddate is null and StartDate <> )

)

and Archive =

ORDER BY StartDate ASC, StartTime ASC

Also, I think your logic cfif was back. Instead of NEQ I think you meant EQ because having an EndDate EQ "and then comparing EndDate until today (else clause) makes no sense.

Tags: ColdFusion

Similar Questions

  • Using decode in where clause

    Hello

    I need to use decode condition in where clause in such a way that if the respective column is null then consider the corresponding column

    for example

    Fee_dateFee_refund_date
    10 OCTOBER 05
    12 NOVEMBER 05
    11 JANUARY 06
    16 FEBRUARY 06

    I have a request in place as below where p_fee_flag will be passed as either FEES or set to null, I check another condition as if p_fee_flag is null then fee_date must be set to null when comparing fee_refund_date between TRUNC (BKT. ACT_START_DATE) AND TRUNC (BKT. ACT_END_DATE

    SELECT 1

    BUCKET BKT

    WHERE DECODE (P_FEE_FLAG, 'FEES', TRUNC(FS.) FEE_DATE), TRUNC (FS. FEE_REFUND_DATE))

    BETWEEN TRUNC (BKT. ACT_START_DATE) AND TRUNC (BKT. ACT_END_DATE)

    944524 wrote:

    Hello Alberto,.

    Thank you that's what I ask but is it possible to verify the condition even using decode

    Yes, it is possible, but why?

    Just replace the query in this way:

    select *
      from fee_table f
    where exists(select 1
                    from bucket b
                  where decode(:p_fee_flag, 'FEE', f.fee_date, f.fee_refund_date) between b.act_start_date and b.act_end_date)
      and decode(:p_fee_flag, 'FEE', null, f.fee_date) is null;
    

    Kind regards.

    Alberto

  • Use of the place where and having clause

    Hi all

    I have always a doubt as to the use of HAVING and WHERE clause.
    Suppose I have table T1 with a single column C1

    CREATE TABLE T1
    (C1 VARCHAR2 (1));

    who data following INSERT scripts

    INSERT INTO T1 VALUES ('A');
    INSERT INTO T1 VALUES ('B');

    INSERT INTO T1 VALUES('C');

    INSERT INTO T1 VALUES ('A');
    INSERT INTO T1 VALUES ('B');

    INSERT INTO T1 VALUES ('A');


    Now I want the result as follows



    C1 = COUNT (C1)
    ==============
    B = 2
    A = 3

    Then out of query 1 and 2, approach who is right?

    (1) SELECT C1, COUNT (C1) FROM T1
    WHERE C1 <>'C '.
    GROUP BY C1
    ORDER BY C1 DESC;

    (2) SELECT C1, COUNT (C1) FROM T1
    GROUP BY C1
    SEEN C1 <>'C '.
    ORDER BY C1 DESC;

    Published by: user13306874 on June 21, 2010 02:36

    In SQL, it is always better to filter data as soon as possible of the moment.
    In your example, the WHERE clause would be right now:

    SQL> explain plan for
      2  select c1,count(c1)
      3  from t1
      4  where c1 != 'C'
      5  group by c1
      6* order by c1 desc;
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display);
    
    PLAN_TABLE_OUTPUT
    ----------------------------------------------------
    Plan hash value: 3946799371
    
    ----------------------------------------------------
    | Id  | Operation          | Name | Rows  | Bytes |
    ----------------------------------------------------
    |   0 | SELECT STATEMENT   |      |     5 |    10 |
    |   1 |  SORT GROUP BY     |      |     5 |    10 |
    |*  2 |   TABLE ACCESS FULL| T1   |     5 |    10 |
    ----------------------------------------------------
    
    Predicate Information (identified by operation id):
       2 - filter("C1"!='C')
    
    18 rows selected.
    
    SQL>
    

    As you can see that the filter is applied during the analysis of T1.

    Whereas in the case of HAVING:

    SQL> explain plan for
      2  select c1,count(c1)
      3  from t1
      4  group by c1
      5  having c1 != 'C'
      6* order by c1 desc;
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display);
    
    PLAN_TABLE_OUTPUT
    ----------------------------------------------------
    Plan hash value: 3146800528
    
    ----------------------------------------------------
    | Id  | Operation           | Name | Rows  | Bytes |
    ----------------------------------------------------
    |   0 | SELECT STATEMENT    |      |     6 |    12 |
    |*  1 |  FILTER             |      |       |       |
    |   2 |   SORT GROUP BY     |      |     6 |    12 |
    |   3 |    TABLE ACCESS FULL| T1   |     6 |    12 |
    ----------------------------------------------------
    
    Predicate Information (identified by operation id):
       1 - filter("C1"!='C')
    
    18 rows selected.
    
    SQL>
    

    The analysis is performed after all groups have been calculated: which was calculated in vain, because it will be further filtered because of the HAVING clause.

    In general, I would like to use as a guide: If you do not use aggregate functions in your predicate in the HAVING clause, set this predicate to the WHERE of your query part.

    Published by: Toon Koppelaars June 21, 2010 11:54

  • Using the index function in where clause of Exchange.

    Hello friends,

    I need your help with a problem.

    I have a query that uses two table Say T1 and T2, where C1 is common column with which both are joined.
    C1 is the primary key in T1, but no index available in Q2 for the C1. T1C2 is the column that we want to select.
    (Note that table may be a Master table)

    Now let's see the query:
    Select T1C2
    From T1, T2
    where T2. C1 = T1. C1

    Here where the clause may have other conditions and From clause can have other tables as needed.

    I want to know that if I have change the query as continuation of leave my query to use the index available of T1. C1.

    Select T1C2
    from T1, T2
    where T1. C1 = T2.C1

    Then, the query uses the index available of T1. and I get better performance. Even a small improvement of performance help me much because this type of query is used in a loop where clause (so it will be run several times).

    Please advise on this...

    Kind regards
    Lifexisxnotxsoxbeautiful...

    Hello

    18:43:17 rel15_real_p>create table t1(c1 number primary key, c2 number);
    
    Table created.
    
    18:43:26 rel15_real_p>create table t2(c1 number, c2 number);
    
    18:45:08 rel15_real_p>
    18:45:09 rel15_real_p>begin
    18:45:09   2  for i in 1..100
    18:45:09   3  loop
    18:45:09   4        insert into t1(c1,c2) values (i,i+100);
    18:45:09   5  end loop;
    18:45:09   6  commit;
    18:45:09   7  end;
    18:45:09   8  /
    
    PL/SQL procedure successfully completed.
    
    18:45:09 rel15_real_p>
    18:45:09 rel15_real_p>
    18:45:09 rel15_real_p>begin
    18:45:09   2  for i in 1..100
    18:45:09   3  loop
    18:45:09   4        insert into t2(c1,c2) values (i,i+200);
    18:45:09   5  end loop;
    18:45:09   6  commit;
    18:45:09   7  end;
    18:45:09   8  /
    
    18:45:23 rel15_real_p>select count(*) from t1;
    
      COUNT(*)
    ----------
           100
    
    18:45:30 rel15_real_p>select count(*) from t2;
    
      COUNT(*)
    ----------
           100
    
    18:45:49 rel15_real_p>select index_name,index_type from user_indexes where table
    _name='T1';
    
    INDEX_NAME                     INDEX_TYPE
    ------------------------------ ---------------------------
    SYS_C0013059                   NORMAL
    
    18:48:21 rel15_real_p>set autotrace on
    18:52:25 rel15_real_p>Select T1.C2
    18:52:29   2  From T1, T2
    18:52:29   3  where T2.C1 = T1.C1
    18:52:29   4  /
    
            C2
    ----------
           101
           102
           103
           104
           105
      .....
      ......
    
            C2
    ----------
           200
    
    100 rows selected.
    
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=7 Card=100 Bytes=
              900)
    
       1    0   HASH JOIN (Cost=7 Card=100 Bytes=3900)
       2    1     TABLE ACCESS (FULL) OF 'T1' (TABLE) (Cost=3 Card=100 By
              es=2600)
    
       3    1     TABLE ACCESS (FULL) OF 'T2' (TABLE) (Cost=3 Card=100 By
              es=1300)
    
    Statistics
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
             21  consistent gets
              0  physical reads
              0  redo size
           1393  bytes sent via SQL*Net to client
            562  bytes received via SQL*Net from client
              8  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
            100  rows processed
    
    18:52:31 rel15_real_p>analyze table t1 compute statistics;
    
    Table analyzed.
    
    18:55:35 rel15_real_p>analyze table t2 compute statistics;
    
    18:55:38 rel15_real_p>set autotrace on
    18:55:42 rel15_real_p>Select T1.C2
    18:55:43   2  From T1, T2
    18:55:45   3  where T2.C1 = T1.C1
    18:55:46   4  /
    
            C2
    ----------
           101
           102
           103
           104
           105
    
      .....
      ......
    
            C2
    ----------
           200
    
    100 rows selected.
    
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=100 Bytes=7
              00)
    
       1    0   MERGE JOIN (Cost=6 Card=100 Bytes=700)
       2    1     TABLE ACCESS (BY INDEX ROWID) OF 'T1' (TABLE) (Cost=2 Ca
              rd=100 Bytes=500)
    
       3    2       INDEX (FULL SCAN) OF 'SYS_C0013059' (INDEX (UNIQUE)) (
              Cost=1 Card=100)
    
       4    1     SORT (JOIN) (Cost=4 Card=100 Bytes=200)
       5    4       TABLE ACCESS (FULL) OF 'T2' (TABLE) (Cost=3 Card=100 B
              ytes=200)
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
             23  consistent gets
              0  physical reads
              0  redo size
           1393  bytes sent via SQL*Net to client
            562  bytes received via SQL*Net from client
              8  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
            100  rows processed
    
    18:56:56 rel15_real_p>Select T1.C2
    18:56:56   2  From T1, T2
    18:56:56   3  where T1.C1 = T2.C1
    18:56:58   4  /
    
            C2
    ----------
           101
           102
           103
           104
           105
    
      .....
      ......
    
            C2
    ----------
           200
    
    100 rows selected.
    
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=100 Bytes=7
              00)
    
       1    0   MERGE JOIN (Cost=6 Card=100 Bytes=700)
       2    1     TABLE ACCESS (BY INDEX ROWID) OF 'T1' (TABLE) (Cost=2 Ca
              rd=100 Bytes=500)
    
       3    2       INDEX (FULL SCAN) OF 'SYS_C0013059' (INDEX (UNIQUE)) (
              Cost=1 Card=100)
    
       4    1     SORT (JOIN) (Cost=4 Card=100 Bytes=200)
       5    4       TABLE ACCESS (FULL) OF 'T2' (TABLE) (Cost=3 Card=100 B
              ytes=200)
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
             23  consistent gets
              0  physical reads
              0  redo size
           1393  bytes sent via SQL*Net to client
            562  bytes received via SQL*Net from client
              8  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
            100  rows processed
    

    -Pavan Kumar N

  • Difference-conditions (join and a Where Clause)

    Hi people,

    I need to clearly agree on what a difference exactly when we put any condition in INNER JOIN and the WHERE Clause.

    I have tried both way and found the same results. Even in the statistics Plan not much differences.  Any help would be appreciated.

    As:

    1 here, I use filter store in the join condition - Inner

    "SELECT i., Gl * Sc1.Item I.

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    And Gl.Location_Id in (1767, 1747,202,1625)

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    And I.Condition_Id! = 325

    2. here I use filter store in Where clause-

    SELECT i., Gl * Sc1.Item I

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    and I.LOCATION_ID in (1767, 1747,202,1625)

    And I.Condition_Id! = 325

    Thank you

    Mark



    Hello

    MarkCooper wrote:

    Hi guys,.

    To reply to all - I understand.

    1. its good practice to use conditions / filter (except CLAUSE) in the WHERE Clause rather Inner join? bon ?

    2. now, in my previous example. We could use the location code in where clause as it was in the two tables.

    What is the best practice to use the code to location here ( 1 /2) ?

    1. here I use filter store in the Inner join condition ( guess the location code is not in the article table)).

    "SELECT i., Gl * Sc1.Item I.

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

      And Gl.Location_Id in (1767, 1747,202,1625)

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    And I.Condition_Id! = 325

    2. here I use filter store in Where clause (assume that the location code is not in the article table)-

    SELECT i., Gl * Sc1.Item I

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

      And Gl.Location_Id in (1767, 1747,202,1625)

    And I.Condition_Id! = 325

    Thank you

    If location_id isn't in the item table, then the join condition

    On Gl.Location_Id = I.Location_Id

    will cause an error.

    Once more, it should not affect results or performance if a condition like

    Gl.Location_Id in (1767, 1747,202,1625)

    is in the clause or the WHERE clause.  No matter if it is be a column called location_id in any other table, or if the same column Gl.Location_Id is used in other conditions.

    As this condition only refers to a table (GI), I recommend you put it in a WHERE clause, just to make the code clearer.

    Yet once, this applies only to the inner joins, not for outer joins and not to CONNECT BY queries.

  • alias in where clause

    Hello

    It is a question of OCA certification.

    1. SQL > SELECT prod_name, prod_list_price, prod_list_price-(prod_list_price*.25) "DISCOUNTED_PRICE".
    PRODUCTS
    WHERE the discounted_price < 10;

    The query generates an error.
    What is the reason for generating fault?
    A. the parentheses should be added to frame the entire expression
    B. the quotes should be removed from the column alias
    C. column aliases must be replaced by the expression in the WHERE clause
    D. column aliases must be capitalized and closed double quotes in the WHERE clause

    Answer: D


    My question is how we can use aliases ("DISCOUNTED_PRICE") in where clause (WHERE "DISCOUNTED_PRICE" < 10)?

    Please specify.

    Hello

    C. response (and I think, you not write review at this time)

    You cannot use alias name in the WHERE clause.

    And if you want to continue to use DISCOUNTED_PRICE in the WHERE clause, you can use view inline.

    SELECT *
    FROM (SELECT prod_name,prod_list_price,prod_list_price-(prod_list_price*.25) "DISCOUNTED_PRICE"
    FROM products
    )
    WHERE  WHERE discounted_price<10;
    

    See you soon,.
    Suri

  • space in where clause

    Hello
    When I run this application it shows no record
    SQL> select * from mas_unit where unit_name!='';
    
    no rows selected
    
    SQL> select * from mas_unit where unit_name='';
    
    no rows selected
    But if I use a space in where clause, it shows the records
    select * from mas_unit where unit_name!=' ';
    
    13364 rows selected.
    Please can someone explain what oracle doing here?

    Thanx

    11 g DBA wrote:
    Hello
    When I run this application it shows no record

    SQL> select * from mas_unit where unit_name!='';
    
    no rows selected
    
    SQL> select * from mas_unit where unit_name='';
    
    no rows selected
    

    But if I use a space in where clause, it shows the records

    select * from mas_unit where unit_name!=' ';
    
    13364 rows selected.
    

    Please can someone explain what oracle doing here?

    Thanx

    Oracle sees the empty string ('') as NULL.
    Use IS NULL or IS NOT NULL instead of = or! =
    documentation http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements005.htm#g194888

    Nulls in Conditions
    A condition that evaluates to UNKNOWN behaves almost like FALSE. For example, a SELECT statement with a condition in the WHERE clause that evaluates to UNKNOWN returns no rows.

  • ADF Faces 11, add where clause to the detail view in the master/detail relationship

    Hello

    I have two display objects only read connected by a link in a relationship master detail.

    They are displayed in a treetable in the form.

    I have a method on the java object impl view master class, I use to add custom where claused based on entries different user and then run the query.

    Works well, master and detail is interview properly.

    But I also want to further filter the detail view (view from destination in the display link). However, as soon as I add whereclause in the detail view against the master
    relationship of detail seems to have disappeared and are no detail record. I also tried to add a view criteria but with the same result.

    Your comments will be appreciated.

    ADF FACES 11g release 1.


    Jan

    You can get the ViewLinkAccessor, then the ViewObject and try to apply the ViewCriteria, followed by executeQuery... what you are already doing.
    Better to paste your code here...

  • where clause with reg_exp

    Hello

    My query is...

    The string is "1,2,3"...

    output must be '1', ' 2 ', ' 3',

    Please help me...!

    Concerning
    SA


    The guru gave the solution...
    select REGEXP_REPLACE('1, 2, 3', '([0-9]+)', '''\1''') from   dual;
    But, I have to use this condition in Where clause on my request. If I put in which the value of the clause does not appear. Any help on that...! Please, I beg you.

    Concerning
    SA

    Hello

    Query: SELECT "' | REPLACE ('1,2,3',',' "',"'). "' val FROM DUAL;

    will convert '1,2,3' string '1', ' 2 ', 3'.

    Run the sub queries first and then run a select statement.

    CREATE TABLE sample_table
    (
    col_a VARCHAR2 (3)
    );

    INSERT INTO sample_table
    VALUES ('1');

    INSERT INTO sample_table
    VALUES ('2');

    INSERT INTO sample_table
    VALUES ('3');

    INSERT INTO sample_table
    VALUES ('4');

    INSERT INTO sample_table
    VALUES ('5');

    COMMIT;

    SELECT col_a
    OF sample_table
    WHERE INSTR ((SELECT ''' ||)) REPLACE ('1,2,3',',' "',"'). "' val
    THE DOUBLE).
    col_a
    ) > 0;

    The select statement above will give you the desired result of "sample_table" as below.

    COL_A
    1
    2
    3

    Kind regards
    Big Boss

  • Expand the view read-only object where Clause

    Hello

    Is there a way I can add another where, condition of the clause to a viewobject unalterable in my bean limit support more query? The view object has a where clause itself clause which I want to thank. However, it is not editable or based on an entity object.

    Hello

    You can use ViewCriteriaRow to add where clause dynamically.

    http://download.Oracle.com/docs/CD/E15051_01/apirefs.1111/e10653/Oracle/JBO/ViewCriteriaRow.html

    In addition, you can also use a variable binding in the where clause and use setWhereClauseParams method before calling executeQuery.

    http://download.Oracle.com/docs/CD/E15051_01/apirefs.1111/e10653/Oracle/JBO/ViewObject.html#setWhereClause (java.lang.String)

    HTH.

    Arun-

  • several conditional parameters in where clause

    How to select records in a table, when I say emp, a parameter for where the condition as
    If empno is known then use in where clause,
    If ename is known then use ename in where clause,
    If both are known then use two parameters in where clause.

    I use Oracle 9i.
    psuedo code.
    ...
    if p_empno is not null and p_ename is not null then
    select * from emp 
    where empno=p_emp and ename = p_ename ;
    elsif p_empno is null and p_ename is not null then
    select * from emp 
    where ename = p_ename ;
    elsif p_empno is not null p_ename is null then
    select * from emp 
    where empno=p_emp ;
    else 
    select * from emp 
    end if ;
    ...
    How can I do this in sql and plsql


    Thank you

    Published by: user10999538 on July 15, 2009 03:29

    Solution

    SELECT *
    FROM   emp
    WHERE  Nvl(empno,-9999) = Coalesce(p_empno,empno,-9999)
           AND Nvl(ename,'X') = Coalesce(p_ename,ename,'X')
    /
     
    

    SS

  • WHERE clause order ox performance question

    I do not understand order of execution of a WHERE clause, the use of a complex database

    I want to write a SELECT statement with the following condition

    ... WHERE (branch = 'main') AND (type = 1) OR (AND the costs-1 = 2).

    My question is

    < 1 > yoyu can use parentheses within a WHERE clause

    < 2 > how you would write such a clause as above if you cannot use parenthesis

    Thank you

    Hello

    Yes, you can have parentheses in a WHERE clause.
    I highly recommend to use when you need to use both AND and or in the same WHERE clause, since

    WHERE  (    x
            AND y
            )
    OR      z
    

    is not the same as

    WHERE   x
    AND     (   y
            OR  z
            )
    

    As others have said:

    ( charge -1 AND charge = 2 )
    

    will cause a compilation error.
    Here's the syntax valid and quite reasonable (although somewhat obscure):

    WHERE     (     branch = 'main'
         AND     type    = 1
         )
    OR          (     charge != -1
            AND     charge !=  2
         )
    
  • long data type in where clause

    Hello

    I need to select in an Oracle table with a column having
    Long data type and Oracle does not use this column in the
    WHERE clause. Doco Oracle suggests that the columns with LARGE data types may
    only be used in the select statement (not in the CASE or any other
    conditional clause).

    Anyone know any work around for this problem?

    What is the way to see the value of long data type?

    How can we use them in where clause?

    We can all utility to display the specified records? There are Lakes of records...


    I ned use it in where clause...

    Thanks and greetings
    VD

    Published by: vikrant dixit on November 23, 2008 22:47

    So can OK we use CLOB/BLOB in where clause?

    Yes, you can query the LOB columns using the package (F.ex.SUBSTR or INSTR) DBMS_LOB functions.

    Why u say that the result is FTS?

    This would result in FTS as you cannot index LARGE data or create a function based on the column index.

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/sql_elements001.htm#sthref149


    LONG columns cannot be indexed.

    No other way to my knowledge.

    You can convert the LOB column with function TO_LOB as described in

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/functions185.htm#i79464

    Published by: Sissi Kandi on November 24, 2008 12:28

  • Using a CASE statement in 'IN' where clause clause

    Hello

    I have a form that users see after they connect with a few items and they type or select values based on the element type. All reports in the application use these values of PAGE 1 in where clause to filter the lines.

    One of these question is "Quarter".  I have a group of radio buttons on the PAGE 1 on the P1_QTR point.

    I need to provide users a way to take the last 4, last 3, last 2 or current quarter and I need to dynamically build a clause WHERE the interactive report based on the selection in the form.

    I am trying to use the CASE as shown below and get the syntax error.

    AND QTR to (when BOX: P1_QTR = 'CURRENT' then ("T4"))

    When: P1_QTR = "LAST" then ("Q3")

    When: P1_QTR = 'LAST TWO' then ('Q3, "Q4")

    When: P1_QTR = 'LAST THREE' then ("Q2", "Q3,' T4 ')

    another ("T1", "T2", "Q3,' T4 ')

    END)

    How can I achieve this?

    Following a simple logic, try

    and QTR to

    (

    Select 'T4' double where: P1_QTR <> 'LAST '.

    Union of all the

    Select 'Q3' to double where: P1_QTR <> "in PROCESS".

    Union of all the

    Select "Q2" double where: P1_QTR not in ('CURRENT', 'LAST', "LAST TWO")

    Union of all the

    Select "Q1" double where: P1_QTR not in ('CURRENT', 'LAST', 'LAST TWO', 'THREE LATEST')

    )

  • Where Clause condition - using CASES in which clause

    I generate data based on certain criteria of dates as below:


    I need to create a report based on the CLOSE_DATE COLUMN,

    If SYSDATE < 15 of the month can generate report on CLOSE_DATE between the 1st to the 15th of the current month
    If SYSDATE > 15th of the month and then generate report on CLOSE_DATE between the 16th of this month and the last day of the next month.

    I tried, but could not get to the query


    SELECT scheme_code, MODEL_NAME, close_date
    Plans OF
    WHERE)
    BOX WHEN to_number (to_char(close_date,'DD')) > 15
    THEN close_date BETWEEN trunc (trunc (SYSDATE, 'MM') + 15) AND last_day (add_months(SYSDATE,1))
    WHEN to_number (to_char(close_date,'DD')) < 15
    THEN close_date BETWEEN trunc(SYSDATE,'MM') AND trunc (trunc(SYSDATE,'MM') + 15)
    (END);

    where nearby date between the two cases when to_number (to_char (close_date, 'DD') > 15 then... else... end and case... when... then... else... end would be the right structure)

    in other words, you can use cases coming with each of these two values for him between the comparison. Do not use case to inject arbitrary clauses

Maybe you are looking for