a logical condition in a where clause clause

Hello

I am trying to retrieve the 'quantity' where ' serial = 1 and "the Serial expiry_date is > sysdate and the amount of it <>0"?

I wrote a select statement, I don't know that I'm right or not, please correct me if I'm wrong by writing the terms in brackets:

SELECT THE AMOUNT IN V_QTY

OF ITEM_DETAIL

WHERE SERIAL = 1

AND (TRUNC (EXPIRY_DATE) > trunc (sysdate))

AND AMOUNT <>0);

is it good?

Hello

Hi Salem wrote:

Hello

I am trying to retrieve the 'quantity' where ' serial = 1 and "the expiry_date of this series is > sysdate and the amount of it <> 0"?

I wrote a select statement, I don't know that I'm right or not, please correct me if I'm wrong by writing the terms in brackets:

SELECT THE AMOUNT IN V_QTY

OF ITEM_DETAIL

WHERE SERIAL = 1

AND (TRUNC (EXPIRY_DATE) > TRUNC (SYSDATE))

AND QUANTITY <> 0);

is it good?

You don't need external all brackets, but they don't really hurt anything.  Leaving them it will not damage allows you to get the wrong results, or make the query more slowly.

If all the conditions are joined by AND then classify parentheses doesn't change anything.  In other words, all the following conditions are equivalent:

  • x AND y AND z
  • (x AND y) AND z
  • x AND (y AND z)

There is also no need to truncate the upper date.  It is not cause wrong results, but according to you the table, it could significantly slower query.  You do not want to call an additional function when you don't need to.  Comparison expressions are sometimes much faster if a column in your table (for example, expiry_date in this case) is the only one on one side of the comparison operator, because the optimizer can use an index in this way.

I would like to do this way:

SELECT quantity

IN v_qty

OF ITEM_DETAIL

WHERE SERIAL = 1

AND EXPIRY_DATE > TRUNC (SYSDATE) - NOT QUITE: see answer #3, below

AND QUANTITY <> 0

;

Tags: Database

Similar Questions

  • 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

  • Conditional function on "where clause".

    Hi guys,.

    How can I get a condition with function on "where to?"

    something like that:

    Get-VM | where{@{N="Path";E={Get-Path -Object $_}} -Like "*SAP*"}
    

    I don't want to get a container with properties of select objects, I want to take an object.

    It works when I call the function Select-object level and then check the result in "where" column

    PS. Sorry for my English I'm not native.

    You could do something like that

    Get-VM | where{(Get-Path -Object $_) -Like "*SAP*"}
    

    But it depends on what the function Get-path is in effect?

    Is it returns a [string] object? If so, the above should work.

  • Outer joins and null in the 'where' clause condition

    Hi people,

    Please help me on this.

    Here's my query.

    with x

    (select 'a' as a dual union all col1)

    Select 'b' as col1 of union double all the

    Select 'c' as double col1

    ),

    y as

    (

    Select 'b' as col2 from dual Union all the

    Select 'c' as col2 from dual Union all the

    Select would be "as col2 from dual Union all the"

    Select 'e' as col2 from dual

    )

    Select * x y right outer join

    on x.col1 = y.col2 and y.col2 is null

    Get all the lines of 'COL1' as null. Why like this?

    Just add the condition to the WHERE clause for example

    WITH x AS
    (SELECT 'a' AS col1 FROM dual UNION ALL
    SELECT 'b' AS col1 FROM dual UNION ALL
    SELECT 'c' AS col1 FROM dual
    ),
    y AS
    (
    SELECT 'b' AS col2 FROM dual UNION ALL
    SELECT 'c' AS col2 FROM dual UNION ALL
    SELECT 'd' AS col2 FROM dual UNION ALL
    SELECT 'e' AS col2 FROM dual
    )
    SELECT * FROM x LEFT OUTER JOIN y
    ON x.col1=y.col2                     ----want to add "and y.col2 is null " condition to get value "a"
    where y.col2 is null
    
  • Max value return value despite where clause is not not really looking for the reason

    Hello

    Using oracle 11.2.0.3

    Select 'A '.

    of the double

    where 1 = 2

    No returns no row, as expected

    Select max ('A')

    of the double

    where 1 = 2 - that is to say isn't true

    Returns the line with null

    Why is this?

    Thank you

    Hello

    user5716448 wrote:

    Hello

    Thanks for the reply.

    I guess that's rule ageneral wait even if no line because of the place where the condition.

    where 1 = 2 - that is to say isn't true

    Thank you

    Yes, if there are no rows in the table, or no line meets the conditions in the WHERE clause, it would be logical that the query return lines exactly 0, but it is also logical that the query returns exactly 1 row.  People who have designed SQL apperantly thought return 1 row was most helpful.

    If you want the query to return 0 rows when there is no data, you can GROUP BY a constant.  For example:

    SELECT MAX (hiredate) AS last_hitedate

    FROM scott.emp

    WHERE deptno = & deptno_wanted

    GROUP 0

    ;

    If there are any rows that meet the condition in the WHERE clause, then the query will produce 1 row, but if no rows meet this condition, then the query will return 0 rows.

    Personally, I'm happy that they designed this way SQL.  I find it rather convenient that the aggregates without GROUP BY clause will return 1 row.  I do things like

    SELECT COUNT (*)

    FROM table_x;

    often, when I want to know if a table has been filled or not.  Seeing a 0 for the output is much clearer that does not get any results.

  • 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.

  • BUSINESS in WHERE Clause?

    Hi all


    I'm on oracle 10 g 2.

    I have the following where clause, whenre I need use cases for one of the filter condition in the where clause... According to one of the input parameters...
    I have the below code segment... The case statement is correct, we can use it here?

    ||
                            LEFT OUTER JOIN RNS_CORE.V_GET_DVMT VAH
                            ON DATE_RANGE.DATE_YEAR = TO_CHAR(VAH.DATA_DATE,'YYYY')
                            CROSS JOIN YEAR_SUB 
                            WHERE
                           ( CASE WHEN p_route_opposite_ind='N' THEN
                                                    ROUTE_ID = p_route_id
                                         ELSE   ( ROUTE_ID = p_route_id
                                                     OR  ROUTE_ID =(SELECT RTE_OPPOSITE_DIRECTION_RTE_ID FROM RNS_CORE.TBL_RTE WHERE RTE_ID=p_route_id))
                                          END CASE );          
                            AND  (( ROUTE_FROM_MEASURE >= P_ROUTE_FROM
                            AND   ROUTE_TO_MEASURE   <= p_route_to_measure)
                            OR (P_ROUTE_FROM BETWEEN ROUTE_FROM_MEASURE AND ROUTE_TO_MEASURE)
                            OR (p_route_to_measure BETWEEN ROUTE_FROM_MEASURE AND ROUTE_TO_MEASURE)
                            ) 
                            AND   DATA_DATE BETWEEN TO_DATE('01/01/'||TO_CHAR(TO_DATE(p_start_date,'MM/DD/YYYY'),'yyyy'),'mm/dd/yyyy')
                            AND TO_DATE('12/31/'||TO_CHAR(TO_DATE(p_end_date,'MM/DD/YYYY'),'yyyy'),'mm/dd/yyyy')
                            --AND DATE_RANGE.DATE_YEAR IS NULL
    
                        );
    Thanks in advance

    Enjoy your sugessions

    HU

    Hello

    You need not case for this. Try something like:

    WHERE   (    (       p_route_opposite_ind     = 'N'     AND route_id = p_route_id )
         OR   ( NVL ( p_route_opposite_ind
                        , 'Y'
                 )                      != 'N'     AND route_id IN (
                                                                         SELECT  rte_opposite_direction_rte_id
                                                    FROM    rns_core.tbl_rte
                                                    WHERE   rte_id  = p_route_id
                                                                      )
                 )
            )
    AND     route_from_measure     >= p_route_from
    AND     ...
    

    You can use the BOX in a WHERE clause. CASE did the same thing in a WHERE clause that he does everywhere else: it returns a single value in one of the types of SQL, such as DATE, NUMBER or VARCHAR2 data. (There is no Boolean data type in SQL).

    See this thread:
    Re: Error on the procedure

  • Details on the order of columns in a Where Clause

    Hi All
    My both Quarries  are giving same record count .
    so  can  i say that the order of the columns("msib.inventory_item_id=pol.item_id" AND" pol.item_id=msib.inventory_item_id ") 
    in the where  clause doesn't effect the query result in any situation 
    {code
    }
    select count(*)
    from po_headers_all poh
       ,po_vendors pv
       ,po_lines_all pol
       ,po_line_locations_all pll
       ,mtl_system_items_b msib
    where poh.vendor_id = pv.vendor_id
    and poh.po_header_id = pol.po_header_id
    and pol.po_line_id = pll.po_line_id
    and pll.ship_to_organization_id = msib.organization_id ----------------1
    and msib.inventory_item_id=pol.item_id --------------------------2
    and poh.type_lookup_code = 'STANDARD'
    and pv.vendor_name = 'Star Gate Ltd'  
    group by 1
    
    select count(*)
    from po_headers_all poh
       ,po_vendors pv
       ,po_lines_all pol
       ,po_line_locations_all pll
       ,mtl_system_items_b msib
    where poh.vendor_id = pv.vendor_id
    and poh.po_header_id = pol.po_header_id
    and pol.po_line_id = pll.po_line_id
    and pol.item_id = msib.inventory_item_id  ----------------------------------2 
    and pll.ship_to_organization_id = msib.organization_id -----------------------------1 
    and poh.type_lookup_code = 'STANDARD'
    and pv.vendor_name = 'Star Gate Ltd'  
    Thank you

    Published by: vijay539 on November 20, 2009 09:17

    Published by: vijay539 on November 20, 2009 09:18

    Published by: vijay539 on November 20, 2009 09:19

    Published by: vijay539 on November 20, 2009 09:19

    Hello

    Right; the order in which the columns appear in the SELECT clause has nothing to do with the lines that are in the result set.

    In fact, unless you say SELECT DISTINCT , I don't think there's anything (except to trigger an error) you can do in the SELECT clause, which would change the lines are selected.

    Published by: Frank Kulash, November 20, 2009 12:45

    Sorry, I misread the question. You may have about the order of the conditions in the WHERE clause, not the SELECT clause.

    The answer in short is the same:
    Right.

    Unless you mix ANDS and ORs (no one should ever do and that you are not), the order of the conditions will make no difference in the results.

  • May I ask where clause to satisfy two conditions at the same time (and)

    Hello

    I wrote a few joins and usually in the final stage where condition with certain conditions be satisfied in where clause. In all conditions where I only want one and condition fulfilled only if both are collected.

    If we look at the this funciton is I should meet (get_order_type (ogr.order_number) <>'R')) AND wh1.third_party_warehouse! = 'Y') but I'm not get rid off both that if order_type is 'R' they are removed and if tooling_group is yes then ther are removed the result. I'm trying with both below pointed out together how can check these conditions? I can do in where clause?

    ((sg.tooling_stock_group <> 'Y'))

    and

    (get_order_type (ogr.order_number) <>'R'))

    Of order_goods_received ogr

    LEFT JOIN part_master pm

    On ogr.part_number = pm.part_number

    -stock_groups sg left join

    SG stock_groups left join

    on pm.stock_group = sg.stock_group

    and ((sg.tooling_stock_group! = 'Y'))

    and (get_order_type (ogr.order_number)! = 'R'))

    -Pm.stock_group = sg.stock_group

    LEFT JOIN batch_record_1 br1

    -A FEW MORE CODE HERE-

    WHERE ((ogr.delivery_date > = trunc (date_départ)))

    AND

    ((ogr.delivery_date < = trunc (end_date)))

    AND

    OGR.goods_received_number <>'1' AND

    get_order_type (OGR.order_number) NOT IN ('L') AND

    OGR.returned_to_supplier_indicator not in('S','L') and

    -sg.tooling_stock_group <>'Y '.

    -((sg.tooling_stock_group <> 'Y') and (get_order_type (ogr.order_number) not in ('R')))

    -(get_order_type (OGR.order_number) <>'R' AND sg.tooling_stock_group <>'Y')

    ((sg.tooling_stock_group <> 'Y'))

    and

    (get_order_type (ogr.order_number) <>'R'))

    AND wh1.third_party_warehouse! = « Y »

    UNION

    (

    Thank you.

    Whenever you have a problem with such logic, you should try and draw a logic state diagram:

    In your case, you are looking to two variables, with two possible States (equal to a value or is not it)

    Then, take this sense of the sg.tooling_stock_group T and O as being the get_order_type (ogr.order_number) and create a state diagram logical based on your description of what you want.  We will use a value of "Y" to indicate we want to record and a value of "N" to indicate the folder should not be returned.

    Start with an empty logical diagram...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O = 'R '.
    |         |         |
    |         |         |


    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    First of all, you said: "If there is the result returned by the defined user function is"R"and the table that has the tooling_stock_group y then it should avoid this record."

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |         | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Then you said: "If order_type does not return" R' but the tooling_stock_group is 'Y', then I should not avoid recording ".

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |         | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    Y    |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Now, you have not given any logic for the rest of the diagram, so I made the assumption that you do not want to avoid these records...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    Y    | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    Y    |    Y    | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Now there are different ways to determine the location of the clause of this diagram.
    We could say that we want the records for all of the T column! = "Y" or all the records for all of the O line! " = R...

    where (sg.tooling_stock_group! = 'Y' or get_order_type (ogr.order_number)! = 'R')

    We say we want to EXCLUDE (using DO NOT) all records where T = "Y" AND O = "O" "

    If not (sg.tooling_stock_group = 'Y' and (ogr.order_number) get_order_type = 'R')

    Those are the two most logical ways to do.
    (In my previous answer, that we can see in this diagram, there is no need of the "or get_order_type (ogr.order_number).) (= 'R' "part, which is superfluous, but would not break it)

    Now let's look at your logic to see what it looks like to in the diagram...

    where ((sg.tooling_stock_group <> «Y»))
    and (get_order_type (ogr.order_number) <> 'R')
    )

    In a logic diagram that looks to...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    N    | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    Y    | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    As you can see, your logic was only targeting one of the 4 possible results for select records and excluding 3.

  • Cannot run a «BUSINESS...» WHEN ' with a date in a WHERE CLAUSE condition

    Hello

    I need to check for a condition and date according to the date, he would check a set of host names or the other game... so I thought I could write something like this in the WHERE clause:

    AND HOST_NAME IN

    (

    CASE

    WHEN TO_DATE (: StopDate, 'MON-DD-YYYY') > = TO_DATE ('22 - DEC - 2015 "," DD-MON-YYYY ') THEN

    ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') ON THE OTHER

    ("SERVERNAME1", "NAMESERVER2", "SERVERNAME3")

    END

    )

    I get the following:

    ORA-00907: lack of right parenthesis

    00907 00000 - "lack the right parenthesis.

    But there is no missing parentheses!

    If I take the case... when... end, and run the host_name in ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') (separately, they run fine)...

    I also tried:

    AND

    (

    CASE

    WHEN TO_DATE (: StopDate, 'MON-DD-YYYY') > = TO_DATE ('22 - DEC - 2015 "," DD-MON-YYYY ') THEN

    HOST_NAME IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') ELSE

    HOST_NAME IN ('SERVERNAME1","NAMESERVER2","SERVERNAME3")

    END

    )

    the error I get is:

    ORA-00905: lack of keyword

    00905 00000 - 'lack the key word'

    What keyword miss me?

    Post edited by: c75d2e42-06a0-4eb1-a576-5652edcbfbe8

    Hello

    c75d2e42-06a0-4eb1-A576-5652edcbfbe8 wrote:

    It was a typo when transferred to the Oracle forum... the version of Oracle is: PL/SQL version 10.2.0.3.0 (10 G)

    I also used IF the statement and that has not worked (in error "missing parenthesis") which I'm sure is a matter of "syntax"... I did not know that the CASE statement returns the value single only, so it's probably the problem.  Is it possible to re - write this?

    ...

    IF only works in PL/SQL.  It does not in SQL, even if that SQL is embedded in PL/SQL.

    There are many ways to re - write the condition.  And here's one:

    AND ((host_name IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC')

    AND TO_DATE (: StopDate, "MON-DD-YYYY") > = TO_DATE ('22-DEC-2015', "MON-DD-YYYY")

    )

    OR (host_name IN ('servername1","Nameserver2","SERVERNAME3")

    AND TO_DATE (: StopDate, "MON-DD-YYYY")<  to_date="" ('dec-22-2015', ="">

    )

    )

    If: StopDate can be NULL, it must be a bit more complicated.

    You can also use nested, such as CASE expressions

    AND THE CASE

    WHEN TO_DATE (: StopDate, "MON-DD-YYYY") > = TO_DATE ('22-DEC-2015', "MON-DD-YYYY")

    WHILE CASE

    WHEN host_name IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC')

    AND THEN 'OK '.

    END

    OTHER CASES

    WHEN host_name IN ('servername1","Nameserver2","SERVERNAME3")

    AND THEN 'OK '.

    END

    END = 'OK '.

    I wonder if this in your application is the best way, however.  If SERVERNAMEA was put into service on December 22, maybe that that fact should be recorded in a table.  If you have a table with 1 row per server, you can consider adding 2 columns DATE to show when the server was first and last used.  Then, rather than use a WHERE clause to test servers appropriate, you could do an inner join.

  • 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.

  • Logical OR (where clause) is not capable of change.

    select * from V$version;
    
    BANNER
    -------------------------------------------------------
    Oracle Database 11g Release 11.2.0.1.0 - Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE    11.2.0.1.0      Production
    TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    
    

    ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy-mm-dd hh24:mi:ss';
    
    

    create table TestTable(ID,DateCol) as
    select 1,date '2011-11-11' from dual union all
    select 2,date '2012-07-13' from dual union all
    select 3,date '2030-12-31' from dual;
    
    

    -- FirstSQL
    SELECT *
      FROM TestTable
     WHERE sysdate <= DateCol
        OR DateCol = date '2011-11-11';
    
    ID  DateCol
    --  -------------------
     3  2030-12-31 00:00:00
    
    

    -- SecondSQL
    SELECT *
      FROM TestTable
     WHERE DateCol = date '2011-11-11'
        OR sysdate <= DateCol;
    
    ID  DateCol
    --  -------------------
     1  2011-11-11 00:00:00
     3  2030-12-31 00:00:00
    
    

    Why return record figures are diff

    Logical OR (where clause) must be able to change.

    (Ex "Where A or B", "where B or" can change)

    I found the logical predicate have diff.

    set autot trace
    

    -- FirstSQL
    SELECT *
      FROM TestTable
     WHERE sysdate <= DateCol
        OR DateCol = date '2011-11-11';
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       1 - filter("DATECOL">=SYSDATE@!)
    
    

    -- SecondSQL
    SELECT *
      FROM TestTable
     WHERE DateCol = date '2011-11-11'
        OR sysdate <= DateCol;
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       1 - filter("DATECOL"=TO_DATE(' 2011-11-11 00:00:00', 'syyyy-mm-dd
                  hh24:mi:ss') OR "DATECOL">=SYSDATE@!)
    
    

    And for 11.2.0.3 as well:

    BANNER
    -----------------------------------------------------------------------------
    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE Production 11.2.0.3.0
    AMT for Linux: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production

    5 selected lines.

    SQL > drop table testtable is serving;

    Deleted table.

    SQL > create table TestTable (ID, DateCol) as
    2 Select 1, date '' 2011-11-11 of all the double union
    3 select 2, '' 2012-07-13 of all the double union
    4 Select 3, date ' 2030-12-31' double.

    Table created.

    SQL >-FirstSQL
    SQL > SELECT *.
    2 from TestTable
    3. WHERE sysdate<=>
    4 GOLD DateCol = date '' 2011-11-11;

    ID DATECOL
    ---------- -------------------
    1 11/11/2011 00:00:00
    3 31/12/2030 00:00:00

    2 selected lines.

    SQL >-SecondSQL
    SQL > SELECT *.
    2 from TestTable
    3 WHERE DateCol = date ' 2011-11-11.
    4 GOLD sysdate<=>

    ID DATECOL
    ---------- -------------------
    1 11/11/2011 00:00:00
    3 31/12/2030 00:00:00

    2 selected lines.

    I believe that I've read about this on Metalink somewhere, have yet to find the document, but I'm sure this is a known bug...

  • Condition within a where condition clause

    Dear friends,
    I am a beginner in PL/SQL and I need your help.
    I'll show you my code (it does not):
    declare
        var_mese VARCHAR2 (2);
    begin
        select max(to_number(MESE)) 
        into var_mese
        from NOC_MONITORAGGIO;
           SELECT count(*)
            FROM noc_sdoc_work a,
                 noc_sdoc b,
                 NOC_MONITORAGGIO
           WHERE NVL(a.CD_STRUTTURA,'x') = NVL(b.CD_STRUTTURA,'x')
             AND NVL(a.CD_SUB_STRUTTURA,'x') = NVL(b.CD_SUB_STRUTTURA,'x')
             AND NVL(a.NR_PRATICA,'x') = NVL(b.NR_PRATICA,'x')
             AND NVL(a.ASL,'x') = NVL(b.ASL,'x')
             AND b.STATO IN ( 1, 2 )         
            and
                if  var_mese in ('03','04','05','06','07','08','09','10','11','12') then
                    NOC_MONITORAGGIO.ANNO_GESTIONE = b.ANNO;
                else
                    NOC_MONITORAGGIO.ANNO_GESTIONE = to_number(b.ANNO)-1;
                end if;
    end;
    My problem is here:
            and
                if  var_mese in ('03','04','05','06','07','08','09','10','11','12') then
                    NOC_MONITORAGGIO.ANNO_GESTIONE = b.ANNO;
                else
                    NOC_MONITORAGGIO.ANNO_GESTIONE = to_number(b.ANNO)-1;
                end if;
    I need a nested where condition

    Thank you
    Leo

    You can code box that using a statement in the WHERE clause:

     and NOC_MONITORAGGIO.ANNO_GESTIONE =
                case
                   when var_mese in ('03','04','05','06','07','08','09','10','11','12') then b.ANNO
                   else to_number(b.ANNO)-1
                end
    
  • dynamic logic in SQL WHERE clause based on the value of the ELEMENT

    Hello

    I have a report based on the following SQL query.
       select dept_no,
           dept_name,
           dept_loc
      from dept
     where dept_loc = :P1_DEPT_LOC
       // If P1_DEPT_LOC is not null, I want the WHERE clause and IF  P1_DEPT_LOC is null, then I don't need the WHERE clause.
       // how can I build this logic in same SQL
    Thank you
    Deepak

    Published by: Deepak_J on March 11, 2010 16:37

    where: P1_DEPT_LOC IS NULL or dept_loc =: P1_DEPT_LOC

    If dept_loc is not null better would be

    where dept_loc = coalesce(:P1_DEPT_LOC,dept_loc)

  • Try adding conditions to clause "and" stored in the table for the where clause, unknown syntax

    I am trying to add additional segments to the 'where' clause, and I don't know exactly how to proceed. I am currently using another table to search for keywords and sometimes there aren't everything. I use this table containing the key words to search the description and associate a symbol with a path.

    The code loops through the keywords with the keywords most associated and try to find links to a symbol then excludes those and search for symbols that correspond to those with less keywords and all. I am able to do this with 6 queries and manually changing the arguments, but I want to loop together so that if the changes need to be made in the future, I won't need to go digging in the code.

    Thanks, but that's ok. I already have a solution for this.

Maybe you are looking for

  • Transfer Photos to iCloud without filling the hard drive

    I'm trying to download pictures and videos up to 1 TB of storage iCloud while using "Optimize Mac Storage" and not deplete my 256 GB Flash storage.  If this is possible, what I am doing wrong?  I've done the research for days and can't find an answer

  • No internet driver after windows 8 to upgrade lower windows 7

    I have a HP pavilion 15-e035tx that comes with pre-installed 8 widows. I have since downgraded to windows 7 Professional, but now the computer does not have the drivers from the internet. I have looked for drivers online but may seem only to find the

  • Messages electronic removal of blackBerry Z10

    When I delete a message from my Z10 and then use my desktop pc to access my email account, deleted email appears again, how this can be removed via my mobile phone?

  • ESXi vmnic connected to 20000 MB when waiting for 40000 MB

    Hi allI have 4 x B200M3 5u2 ESXi running blades in a single chassis UCS. They run a number of vSwitches, and all the connected vmnic show 40000 full duplex Mb (as expected).Yesterday, I added 2 B200M3 more (to slot 7 and 8), and I get only 20000 Mb o

  • Adobe Stock extended license?

    What is the price for the license for Adobe Stock photos so that they can be used on the mugs, t-shirt and other merchandise?