Using "not in" in where clause yields unexpected results

I'm running against a 10gR2 database, recently upgraded from 9i. I'm running queries in a 3rd party tool (not SQL Plus).
I confirmed the behavior with the following query. The query returns no rows, although I’d expect it to return the 2. So “NOT IN” is behaving like “!=”, where you can't ask if a value != null.
select a
from (select 1 a from dual
union
select 2 from dual
union
select 3 from dual)
where a not in
(select 1 from dual
union
select null from dual
union
select 3 from dual)
The following query, which simply eliminates the NULL row, returns the “2” as expected.
select a
from (select 1 a from dual
union
select 2 from dual
union
select 3 from dual)
where a not in
(select 1 from dual
union
select 3 from dual)
I hadn't seen this behavior before. We recently migrated from 9i to 10g, so I'm wondering - is this new to 10g or is it that I simply haven't seen it before? I don't often use "NOT IN".
Is it expected behavior?

Hi,
It's expected, if not intuitive, at least if you're not used to SQL's 3-value logic. All Oracle versions act the same in this matter.
See [this thread|http://forums.oracle.com/forums/thread.jspa?messageID=3584729&#3584729] for an explanation.

Similar Messages

  • How can we use DECODE function in where clause.

    Hi Guys,
    I have to use DECODE function in where clause.
    like below
    select * from tab1,tab2
    where a.tab1 = b.tab2
    and decode(code, 'a','approved')
    in this manner its not accepting?
    Can any one help me on this or any other aproach?
    Thanks
    -LKR

    >
    I am looking for to decode the actual db value something in different for my report.
    like if A then Accepted
    elseif R then Rejected
    elseif D then Denied
    these conditions I have to check in where clause.
    >
    what are you trying to do?
    may be you are looking for
    select * from tab1,tab2
    where a.tab1 = b.tab2
    and
       (decode(:code, 'A','Accepted') = <table_column>
        or
        decode(:code, 'R','Rejected') = <table_column>
       or
        decode(:code, 'D','Denied') = <table_column>
       )

  • How to use Alias Columns in WHERE CLAUSE

    Hi ,
    I have a query where in there are 2 alias columns, start_date and end_date where in i need to use them in my WHERE clause as start_date < end_date. Please let me know if this is possible. please see the bwlow query
    SELECT (GREATEST (MIN (a.start_date_active),
    MIN (b.start_date_active),
    MIN (c.start_date_active),
    d.start_date_active ) start_date ,
    LEAST (MAX (NVL (a.end_date_active, b.end_date_active)),
    MAX (c.end_date_active),
    MAX (b.end_date_active),
    NVL (d.end_date_active,'31-DEC-2099')) end_date,
    c.terr_id,
    a.source_number,
    e.resource_id mgr_resource_id,
    d.role_relate_id role_relate_id
    ,g.resource_id
    FROM table1 a,
    table5 e,
    table4 d,
    table6 f,
    table7 g,
    table8 h ,
    table2 b,
    table3 c,
    table9 i
    WHERE 1=1
    AND b.resource_id = g.resource_id
    AND c.terr_id = b.terr_id
    AND to_number (c.attribute3) = i.party_id
    AND a.source_number = UPPER (e.salesrep_number)
    AND d.role_resource_type = 'RS_INDIVIDUAL'
    AND e.resource_id = d.role_resource_id
    AND d.role_id = f.role_id
    AND (a.start_date_active <= b.end_date_active
    AND (NVL (a.end_date_active, b.end_date_active) >= b.start_date_active))
    AND h.resource_id = a.resource_id
    AND UPPER (g.salesrep_number) = h.source_number
    GROUP BY a.source_number, c.terr_id, e.resource_id,d.start_date_active,d.end_date_active,d.role_relate_id,g.resource_id
    Thanks,
    Lakshmi

    I did not understand your query but have you tried using the HAVING clause?
    HAVING clause allows you to use a "where" like condition for your groupings.
    See http://www.techonthenet.com/sql/having.php for some examples.
    Sandeep Gandhi

  • How to use alias name in where clause

    Hello,
    DECODE (item.inv_type,'OT', (DECODE (item.attribute2, 'STONE', 0, xfer.release_quantity1 * xfer.attribute10)
    'FG', (xfer.release_quantity1 * xfer.attribute10)
    ) matl_val
    In the above code matl_val is alias name i need to use that one in where clause as
    where matl_val > 0
    is this possible or anyother way can anyone help me.

    But the point is as you haven't read the documentation you may miss some valuable points about alias and will soon end with another problem.
    >
    Specify an alias for the column expression. Oracle Database will use this alias in the column heading of the result set. The AS keyword is optional. The alias effectively renames the select list item for the duration of the query. The alias can be used in the order_by_clause but not other clauses in the query.
    >
    http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/statements_10002.htm#SQLRF01702

  • Case Working in "TOAD" but not Working in "WHERE" clause

    Hi Friends,
    When I am using case in the where clause of SQL query in Oracle Reports 3.0.5, than it is giving me "Error - Rep - 0946 Unable to Parse Query"
    But when I am running the same query in TOAD it is working fine.
    select *from inv_stores, inv_product, inv_location
    where  (inv_stores.location_code in ('12', '13', '16', '17', '18') and
                inv_stores.location_code = inv_location.location_code and
                inv_stores.department_code = inv_product.department_code and
                inv_stores.product_code = inv_product.product_code and
                nvl(delete_flag, 'N') <> 'Y' and
                inv_product.department_code = '11' and
                       (((CASE WHEN nvl(inv_product.sale_price3jd,0) = 0 THEN 0  ELSE (nvl(inv_product.sale_price3jd,0) - nvl(inv_product.cost_pricejd,0)) /
                       nvl(inv_product.sale_price3jd,0) END) *  100 ) <= 100))
    order by inv_product.department_code, inv_product.product_codePlease Help me.......
    Thanks in Advance

    Hello,
    When you create a SQL query in Reports, Reports will parse the SQL query in order to create the "data model".
    Reports 3.0.5 is quite old, so, you can use only the SQL "supported" by the RSF included in Reports 3.0.5.
    When you use TOAD , the sql query is not parsed by TOAD but by the DB just like with sqlplus or SQL Developer.
    Regards

  • Using 2 catsearch in where clause

    Hi all,
    I have problem using 2 catsearch in where clause, am not sure if there is any solution this. i have created a simple test data as below:-
    -- Test Code
    create table cust_catalog (
    id number(16),
    firstname varchar2(80),
    surname varchar2(80),
    birth varchar2(25),
    age numeric )
    INSERT ALL
    INTO cust_catalog VALUES ('1','John','Smith','Glasgow','52')
    INTO cust_catalog VALUES ('2','Emaily','Johnson','Aberdeen','55')
    INTO cust_catalog VALUES ('3','David','Miles','Leeds','53')
    INTO cust_catalog VALUES ('4','Keive','Johnny','London','45')
    INTO cust_catalog VALUES ('5','Jenny','Smithy','Norwich','35')
    INTO cust_catalog VALUES ('6','Andy','Mil','Aberdeen','63')
    INTO cust_catalog VALUES ('7','Andrew','Smith','London','64')
    INTO cust_catalog VALUES ('8','John','Smith','London','54')
    INTO cust_catalog VALUES ('9','John','Henson','London','56')
    INTO cust_catalog VALUES ('10','John','Mil','London','58')
    INTO cust_catalog VALUES ('11','Jon','Smith','Glasgow','57')
    INTO cust_catalog VALUES ('12','Jen','Smith','Glasgow','60')
    INTO cust_catalog VALUES ('13','Chris','Smith','Glasgow','59')
    SELECT * FROM DUAL
    EXEC CTX_DDL.create_index_set('cust_iset');
    EXEC CTX_DDL.CREATE_PREFERENCE ('cust_lexer', 'BASIC_LEXER');
    EXEC CTX_DDL.SET_ATTRIBUTE('cust_lexer', 'SKIPJOINS' , ',''."+-()/');
    EXEC CTX_DDL.Create_Preference('cust_wildcard_pref', 'BASIC_WORDLIST');
    EXEC CTX_DDL.set_attribute('cust_wildcard_pref', 'prefix_index', 'YES');
    EXEC CTX_DDL.ADD_INDEX('cust_iset','id');
    EXEC CTX_DDL.ADD_INDEX('cust_iset','birth');
    EXEC CTX_DDL.ADD_INDEX('cust_iset','age');
    CREATE INDEX FIRSTNAME_IDX ON cust_catalog(firstname) INDEXTYPE IS CTXSYS.CTXCAT
    PARAMETERS ('index set cust_iset LEXER cust_lexer Wordlist cust_wildcard_pref');
    CREATE INDEX SURNAME_IDX ON cust_catalog(surname) INDEXTYPE IS CTXSYS.CTXCAT
    PARAMETERS ('index set cust_iset LEXER cust_lexer Wordlist cust_wildcard_pref');
    EXEC DBMS_STATS.GATHER_TABLE_STATS('WORKAROUND', 'CUST_CATALOG', cascade=>TRUE);
    -- For removing test data
    drop table cust_catalog;
    EXEC CTX_DDL.DROP_INDEX_SET('cust_iset');
    EXEC CTX_DDL.DROP_PREFERENCE('cust_lexer');
    EXEC CTX_DDL.DROP_PREFERENCE('cust_wildcard_pref');
    DROP INDEX FIRSTNAME_IDX ;
    DROP INDEX SURNAME_IDX ;
    ------- QUESTIONS IN HERE -------------------------------------------------
    SELECT * FROM cust_catalog WHERE ctxsys.catsearch (firstname, 'John','age BETWEEN 40 AND 70 AND birth=''Glasgow''')>0
    I have no problem running above query
    BUT if i add 2 catsearch on both firstname and surname, i have error ~ catsearch does not support function invocation
    SELECT * FROM cust_catalog WHERE ctxsys.catsearch (firstname, 'John','age BETWEEN 40 AND 70 AND birth=''Glasgow''')>0 AND
    ctxsys.catsearch (surname, 'Smith','age BETWEEN 40 AND 70 AND birth=''Glasgow''')>0
    :(

    The following expands the example to include the birth (as part of the multi_column_datastore) and age (using filter by during index creation and sdata in the query) and show the execution plan. I have provided the script and execution separately.
    -- script:
    DROP TABLE cust_catalog
    EXEC CTX_DDL.DROP_PREFERENCE ('cust_lexer')
    EXEC CTX_DDL.DROP_PREFERENCE ('cust_wildcard_pref')
    EXEC CTX_DDL.DROP_PREFERENCE ('your_datastore')
    EXEC CTX_DDL.DROP_SECTION_GROUP ('your_sec')
    CREATE TABLE cust_catalog
      (id         NUMBER   (16),
       firstname  VARCHAR2 (80),
       surname    VARCHAR2 (80),
       birth      VARCHAR2 (25),
       age        NUMERIC)
    INSERT ALL
      INTO cust_catalog VALUES (1,  'John',   'Smith',   'Glasgow',  52)
      INTO cust_catalog VALUES (2,  'Emaily', 'Johnson', 'Aberdeen', 55)
      INTO cust_catalog VALUES (3,  'David',  'Miles',   'Leeds',    53)
      INTO cust_catalog VALUES (4,  'Keive',  'Johnny',  'London',   45)
      INTO cust_catalog VALUES (5,  'Jenny',  'Smithy',  'Norwich',  35)
      INTO cust_catalog VALUES (6,  'Andy',   'Mil',     'Aberdeen', 63)
      INTO cust_catalog VALUES (7,  'Andrew', 'Smith',   'London',   64)
      INTO cust_catalog VALUES (8,  'John',   'Smith',   'London',   54)
      INTO cust_catalog VALUES (9,  'John',   'Henson',  'London',   56)
      INTO cust_catalog VALUES (10, 'John',   'Mil',     'London',   58)
      INTO cust_catalog VALUES (11, 'Jon',    'Smith',   'Glasgow',  57)
      INTO cust_catalog VALUES (12, 'Jen',    'Smith',   'Glasgow',  60)
      INTO cust_catalog VALUES (13, 'Chris',  'Smith',   'Glasgow',  59)
    SELECT * FROM DUAL
    EXEC CTX_DDL.CREATE_PREFERENCE ('cust_lexer', 'BASIC_LEXER')
    EXEC CTX_DDL.SET_ATTRIBUTE ('cust_lexer', 'SKIPJOINS' , ',''."+-()/')
    EXEC CTX_DDL.Create_Preference ('cust_wildcard_pref', 'BASIC_WORDLIST')
    EXEC CTX_DDL.set_attribute ('cust_wildcard_pref', 'prefix_index', 'YES')
    EXEC CTX_DDL.CREATE_PREFERENCE ('your_datastore', 'MULTI_COLUMN_DATASTORE')
    EXEC CTX_DDL.SET_ATTRIBUTE ('your_datastore', 'COLUMNS', 'firstname, surname, birth')
    EXEC CTX_DDL.CREATE_SECTION_GROUP ('your_sec', 'BASIC_SECTION_GROUP')
    EXEC CTX_DDL.ADD_FIELD_SECTION ('your_sec', 'firstname', 'firstname', TRUE)
    EXEC CTX_DDL.ADD_FIELD_SECTION ('your_sec', 'surname', 'surname', TRUE)
    EXEC CTX_DDL.ADD_FIELD_SECTION ('your_sec', 'birth', 'birth', TRUE)
    CREATE INDEX context_idx
    ON cust_catalog (firstname)
    INDEXTYPE IS CTXSYS.CONTEXT
    FILTER BY age
    PARAMETERS
      ('DATASTORE      your_datastore
        SECTION GROUP  your_sec
        LEXER          cust_lexer
        WORDLIST       cust_wildcard_pref')
    COLUMN firstname FORMAT A10
    COLUMN surname   FORMAT A10
    COLUMN birth     FORMAT A10
    SET AUTOTRACE ON EXPLAIN
    SELECT * FROM cust_catalog
    WHERE  CONTAINS
             (firstname,
              '(John WITHIN firstname)
                AND (Glasgow WITHIN birth)
                AND (SDATA (age BETWEEN 40 AND 70))') > 0
    SET AUTOTRACE OFF-- execution:
    SCOTT@orcl_11gR2> DROP TABLE cust_catalog
      2  /
    Table dropped.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.DROP_PREFERENCE ('cust_lexer')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.DROP_PREFERENCE ('cust_wildcard_pref')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.DROP_PREFERENCE ('your_datastore')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.DROP_SECTION_GROUP ('your_sec')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> CREATE TABLE cust_catalog
      2    (id        NUMBER   (16),
      3       firstname  VARCHAR2 (80),
      4       surname    VARCHAR2 (80),
      5       birth        VARCHAR2 (25),
      6       age        NUMERIC)
      7  /
    Table created.
    SCOTT@orcl_11gR2> INSERT ALL
      2    INTO cust_catalog VALUES (1,  'John',   'Smith',   'Glasgow',  52)
      3    INTO cust_catalog VALUES (2,  'Emaily', 'Johnson', 'Aberdeen', 55)
      4    INTO cust_catalog VALUES (3,  'David',  'Miles',   'Leeds',    53)
      5    INTO cust_catalog VALUES (4,  'Keive',  'Johnny',  'London',   45)
      6    INTO cust_catalog VALUES (5,  'Jenny',  'Smithy',  'Norwich',  35)
      7    INTO cust_catalog VALUES (6,  'Andy',   'Mil',       'Aberdeen', 63)
      8    INTO cust_catalog VALUES (7,  'Andrew', 'Smith',   'London',   64)
      9    INTO cust_catalog VALUES (8,  'John',   'Smith',   'London',   54)
    10    INTO cust_catalog VALUES (9,  'John',   'Henson',  'London',   56)
    11    INTO cust_catalog VALUES (10, 'John',   'Mil',       'London',   58)
    12    INTO cust_catalog VALUES (11, 'Jon',    'Smith',   'Glasgow',  57)
    13    INTO cust_catalog VALUES (12, 'Jen',    'Smith',   'Glasgow',  60)
    14    INTO cust_catalog VALUES (13, 'Chris',  'Smith',   'Glasgow',  59)
    15  SELECT * FROM DUAL
    16  /
    13 rows created.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.CREATE_PREFERENCE ('cust_lexer', 'BASIC_LEXER')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.SET_ATTRIBUTE ('cust_lexer', 'SKIPJOINS' , ',''."+-()/')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.Create_Preference ('cust_wildcard_pref', 'BASIC_WORDLIST')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.set_attribute ('cust_wildcard_pref', 'prefix_index', 'YES')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.CREATE_PREFERENCE ('your_datastore', 'MULTI_COLUMN_DATASTORE')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.SET_ATTRIBUTE ('your_datastore', 'COLUMNS', 'firstname, surname, birth')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.CREATE_SECTION_GROUP ('your_sec', 'BASIC_SECTION_GROUP')
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.ADD_FIELD_SECTION ('your_sec', 'firstname', 'firstname', TRUE)
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.ADD_FIELD_SECTION ('your_sec', 'surname', 'surname', TRUE)
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> EXEC CTX_DDL.ADD_FIELD_SECTION ('your_sec', 'birth', 'birth', TRUE)
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> CREATE INDEX context_idx
      2  ON cust_catalog (firstname)
      3  INDEXTYPE IS CTXSYS.CONTEXT
      4  FILTER BY age
      5  PARAMETERS
      6    ('DATASTORE     your_datastore
      7        SECTION GROUP     your_sec
      8        LEXER          cust_lexer
      9        WORDLIST     cust_wildcard_pref')
    10  /
    Index created.
    SCOTT@orcl_11gR2> COLUMN firstname FORMAT A10
    SCOTT@orcl_11gR2> COLUMN surname   FORMAT A10
    SCOTT@orcl_11gR2> COLUMN birth        FORMAT A10
    SCOTT@orcl_11gR2> SET AUTOTRACE ON EXPLAIN
    SCOTT@orcl_11gR2> SELECT * FROM cust_catalog
      2  WHERE  CONTAINS
      3             (firstname,
      4              '(John WITHIN firstname)
      5             AND (Glasgow WITHIN birth)
      6             AND (SDATA (age BETWEEN 40 AND 70))') > 0
      7  /
            ID FIRSTNAME  SURNAME    BIRTH             AGE
             1 John       Smith      Glasgow            52
    1 row selected.
    Execution Plan
    Plan hash value: 495863752
    | Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT            |              |     1 |   136 |     4   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| CUST_CATALOG |     1 |   136 |     4   (0)| 00:00:01 |
    |*  2 |   DOMAIN INDEX              | CONTEXT_IDX  |       |       |     4   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - access("CTXSYS"."CONTAINS"("FIRSTNAME",'(John WITHIN firstname)
                  AND (Glasgow WITHIN birth)             AND (SDATA (age BETWEEN 40 AND 70))')>0)
    Note
       - dynamic sampling used for this statement (level=2)
    SCOTT@orcl_11gR2> SET AUTOTRACE OFF

  • Use "ListContains' in a WHERE clause

    I believe I have a syntax problem and could you some help.
    First, I have a field in an Access DB called 'degree" that conatins
    a list of numbers. This list can be any combination of the numbers
    4 through 32. On my search page I have a selection box in a form to
    select a value of the degree to search for. The variable passed
    from the form is called "degreeValue". I have a component that lets
    me build my WHERE clause using several seach criteria. It functions
    perfectly prior to adding the search for "degree". I attempted to
    use "ListContains" in the WHERE clause using "degree" as the name
    of my list and "degreeValue" as my substring to search for. The
    error I get says that "variable degree is undefined". I am
    attaching the full code of the compoanent so you can see it. Again,
    it works if you take out the subclause build for "degree".

    I understand what you are saying and in other situations have
    done my tables that way. I didn't this time because this is one of
    those... "add it later" kind of things. Yes, the field does look
    like "1,2,3,4,etc.." The inventory a couple thousand items each
    with a number (catno). As you obviously noticed the inventory table
    houses most of the data for the items. The "degree" field in the
    past was only used to populate a series of checkboxes and was not
    searchable, thus a list was used.
    With a possibility of 29 values for "degree" and a
    possiblility of over 2,000 inventory items you can see a newly
    created table might become quite large. It would be nice if I could
    use the current structure.
    You are absolutely correct saying the design should be
    different. Thank you for your quick reply and good information.

  • Using CLOB datatypes in WHERE clause

    Hi All,
    I have a table with two columns as CLOB datatype. I'm using Oracle 8i Enterprise Edition. I can do a query, insert, update and even delete the data in the CLOB field using Oracle's SQL Plus.
    What I want is to do a search on those fields.. that is include that field in a WHERE clause. I'm using this datatype to store large number of data.
    I'd like to see this query working...
    SELECT * FROM MyTable WHERE CLOBFLD LIKE 'Something...';
    Now this query doesn't work. It returns: 'Inconsistent datatype' near the word CLOBFLD.
    Please Help me out.
    Regards,
    Gopi
    null

    I presume you want to query based on the contents of the CLOB, right ? If that is true, then you have to create a text index, using Oracle Context and then use "Contains" in the where clause to query. Hope this helps.

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

  • Can I use SYSDATE in the WHERE clause to limit the date range of a query

    Hi,
    Basicaly the subject title(Can I use SYSDATE in the WHERE clause to limit the date range of a query) is my question.
    Is this possible and if it is how can I use it. Do I need to join the table to DUAL?
    Thanks in advance.
    Stelios

    As previous poster said, no data is null value, no value. If you want something, you have nvl function to replace null value by an other more significative value in your query.<br>
    <br>
    Nicolas.

  • Is it possible to use LONG columns in WHERE clause or ORDER BY?

    Is it possible to use LONG columns in WHERE clause or ORDER BY?

    Hi,
    LONG data type is deprecated, maybe could you change your column type to LOB ?
    Nonetheless below is a workaround which may fit your needs if forced to use LONG.
    It uses a function which returns you a CLOB. It allows you to use the converted "LONG" column in a WHERE clause.
    Then if you want to order by you have to convert the CLOB to a VARCHAR using DBMS_LOB.SUBSTR.
    SQL> CREATE TABLE my_table (id NUMBER, description LONG);
    Table created.
    SQL> INSERT INTO my_table VALUES (1, 'FIRST LONG');
    1 row created.
    SQL> INSERT INTO my_table VALUES (2, 'ANOTHER LONG');
    1 row created.
    SQL> COMMIT;
    Commit complete.
    SQL> CREATE TYPE my_type_row AS OBJECT (id INTEGER, description CLOB);
      2  /
    Type created.
    SQL> CREATE TYPE my_type_table AS TABLE OF my_type_row;
      2  /
    Type created.
    SQL> CREATE OR REPLACE FUNCTION get_my_long
      2     RETURN my_type_table
      3     PIPELINED
      4  AS
      5     v_tab   my_type_table := my_type_table ();
      6  BEGIN
      7    FOR cur IN (SELECT id, description FROM my_table)
      8  LOOP
      9        PIPE ROW (my_type_row (cur.id, cur.description));
    10  END LOOP;
    11  RETURN;
    12  END;
    13  /
    Function created.
    SQL> SELECT
      2     id,
      3     description
      4  FROM
      5     TABLE (get_my_long ())
      6  WHERE
      7     description LIKE '%LONG'
      8  ORDER BY
      9     DBMS_LOB.SUBSTR(description);
      ID DESCRIPTION
       2 ANOTHER LONG
       1 FIRST LONG
    SQL> SELECT
      2     id,
      3     description
      4  FROM
      5     TABLE (get_my_long ())
      6  WHERE
      7     description LIKE 'FI%';
      ID DESCRIPTION
       1 FIRST LONG
    SQL>Kind regards,
    Ludovic

  • Using Parameters in SQL-Query not only in where clauses

    Hi,
    I try to use Publisher parameters in the SQL Query from a Data Set.
    All of them have default values.
    So far, this is no problem, unless I try to use such a parameter value as an ordinary attribute value:
    >
    select
         case when (:pv_Group = 'no') then "DM15D_BETRIEBSTEIL"."BETR_TEIL"
                                            else :pv_some_Text end                                                                  as Betr_Teil,
         case when (:pv_Group = 'no') then "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM"
                                            else :pv_some_Text end                                                        as Suva_Nr,
         case when (:pv_Group = 'no') then "DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH"
                                            else sum("DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH") end      as Vollbesch
    from "GDWH05"
    where
         "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM" in (:pv_nim100)
         fetch first 65001 rows ONLY
    >
    The parameters 'pv_Group' and 'pv_nim100' are working fine. (when or where clauses)
    The parameter 'pv_some_Text' unfortunately not. (simple literals)
    When I try to validate the above SQL, I get the following Error:
    <font color="red">
    java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS. [nQSError: 46033] Datatype: 25 is not supported.
    </font>
    After use a cast function:
    >
    else CAST(:pv_some_Text AS CHARACTER)
    >
    I get this ERROR:
    <font color="red">
    java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS. [nQSError: 19002] Incorrect use of parameters. The parameters used in CAST cannot be resolved without ambiguity.
    </font>
    We use OBIEE 11.1.1.6.4 on a Win64-System.
    Thank's for any help.

    Hi Alex,
    let's leave away any unnecessary details.
    This is the SQL, inserted in the window 'Edit Data Set' of BIP Data Model:
    >
    select
         '--1'     as Betr_Teil,
         '--2'      as Suva_Nr,
         sum("GDWH05"."DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH") as Vollbesch
    from "GDWH05"
    where
         "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM" in ('122-4.4')
         fetch first 65001 rows ONLY
    >
    Everything is fine when I click OK, the script goes back to the metadata.
    Let's try this script with bind values in ORACLE SQL Developer.
    This SQL is the physical part, found in the OBIEE-Log (Log level 5), except the bind values. Therefore we find, in the where clause, the join. In the logical sql, we don't have to join, because it's handled in the Common Enterprise Information Model (CEIM)
    >
    with
    sawith0 as
    select
    sum(t39617.ris_vollbesch) as c1
    from
    dm15d_betriebsteil t39455,
    dm15k_ris_fakten_pro_btt_jhr t39617
    where
    t39455.id_betriebsteil = t39617.id_betriebsteil
    and t39455.suva_nr_form = '122-4.4'
    select
    d1.c1 as c1,
    d1.c2 as c2,
    d1.c3 as c3
    from
    select
    :pv_some_text as c1,
    :pv_some_text as c2,
    sum(d1.c1) as c3
    from
    sawith0 d1
    d1
    where
    rownum <= 65001
    >
    This SQL works fine, even with bind values for 'pv_some_text'.
    But, when using the following SQL in the BIP Data Model:
    >
    select
         :pv_Text as Betr_Teil,
         :pv_Text as Suva_Nr,
         sum("GDWH05"."DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH") as Vollbesch
    from "GDWH05"
    where
         "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM" in ('122-4.4')
         fetch first 65001 rows ONLY
    >
    The following ERROR occurs:
    <font color="red">java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS. [nQSError: 46008] Internal error: File server\Query\Optimizer\ServiceInterfaceMgr\Utility\Src\SQOIUTypeVisitor.cpp, line 643.</font>
    In my opinion, either I use a wrong syntax, or BIP has a problem with parsing the script.
    Thank you for your most welcome help.

  • Using regexp_instr in a where clause - invalid relational operator

    Whey I try to run this query in TOAD I get an ORA-00920: invalid relational operator error. It's part of a 10g stored procedure. When I highlight it and run it it prompts me for the missing values and then the error pops up. The AND in line 4 is highlighted.
    select CRIME_CLASSIFICATION_ID, crime_type, nvl(count(CRIME_CLASSIFICATION_ID),0) as CRIMECNT
    From vaps.vw_offenses
        where  regexp_instr(valoc,to_char(location_id))
            AND ( fromdate is null or
             offense_date between to_date(fromdate, 'mm/dd/yyyy')  AND to_date(todate,'mm/dd/yyyy')
    group by crime_classification_id, crime_type

    Hi,
    Review what REGEXP_INSTR does: it returns a NUMBER.
    Your WHERE clause couldn't make any sense if you used any other kind of NUMBER expression in that place, e.g. a NUMBER literal such as 12:
    select CRIME_CLASSIFICATION_ID, crime_type, nvl(count(CRIME_CLASSIFICATION_ID),0) as CRIMECNT
    From vaps.vw_offenses
        where  12     -- This is obviously wrong
            AND ( fromdate is null or
             offense_date between to_date(fromdate, 'mm/dd/yyyy')  AND to_date(todate,'mm/dd/yyyy')
    group by crime_classification_id, crime_type
    It's not going to work any better with a function (like REGEXP_INSTR) that returns a NUMBER.
    How can you fix it?  That depends on what you want to do.  Why are you calling REGEXP_INSTR?  What is that condition checking?
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    Always say which version of Oracle you're using (for example, 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Using :case when  in where clause

    Hello,
    I need some help with using of case statement in a where clause
    Table that contains info about employees taking some coursework:
    Class (values could be SAP, ORACLE, JAVA)
    Code (if Class is SAP then CODE is not null; if class is any other CODE is NULL)
    Start Date (date they began class not null)
    End Date (date then ended the class - could be null)
    Employee Level(numbers from one through five)
    I need a single LOV in forms that should show Employee_Level for input class,code,date.
    How to do this?
    I started off with this but get 'missing statement error'
    select distinct employee_level from e_course
       where (
       case when &class='SAP' then code ='1' and start_date > to_date('20000101','YYYYMMDD') and
                                               end_date < to_date('20000101','YYYYMMDD')
               else
                   null
       end) order by employee_level;Thanks

    Hi,
    user469956 wrote:
    But all these examples have only one condition for each case.Depending on how you count, all WHERE clauses have only one condition.
    I see an example in that thread that has 6 atomic conditions, linked by AND and OR.
    I need to check date & code. This is what is causing the error.Why do you want to use a CASE statement?
    Why can't you put all your conditions directly iinto a WHERE clause, soemthing like this:
    WHERE   (   &class='SAP'
            AND code ='1'
    OR      (   start_date  > to_date('20000101','YYYYMMDD')
            AND end_date    < to_date('20000101','YYYYMMDD')
            )I said "something like this" because I don't know what you really want to do.

  • DECODE is not working in WHERE clause when subquery returns more rows

    Hi Gurus,
    I want to write a query on CCENTERS table(Script given below) and expect the following result:
    1. When I pass a value of 0 for ID, It returns all the rows given in the table.
    2. When I pass a value other than 0, It returns the row for the given value as well as all its child records.
    CCENTER has parent-child relationship in ID and BASE column. I am using a query with DECODE function. but DECODE function in WHERE clause is not capable of handling sub-query with multiple rows.
    VARIABLE ParaCCenter NUMBER
    BEGIN
    :paraccenter:=0;
    END;
    CREATE TABLE ccenters
    (id NUMBER,
    name VARCHAR2(20),
    base number);
    INSERT INTO ccenters VALUES(1,'NUST',null);
    INSERT INTO ccenters VALUES(2,'SEECS',1);
    INSERT INTO ccenters VALUES(3,'NBS',1);
    commit;
    SELECT * FROM ccenters
    WHERE id IN DECODE(:ParaCCenter, 0, id,
    (SELECT id FROM ccenters
    START WITH base=:ParaCCenter
    CONNECT BY PRIOR id = base
    UNION
    SELECT :ParaCCenter FROM dual
    BEGIN
    :paraCCenter:=1;
    END;
    SELECT * FROM ccenters
    WHERE id IN DECODE(:ParaCCenter, 0, id,
    (SELECT id FROM ccenters
    START WITH base=:ParaCCenter
    CONNECT BY PRIOR id = base
    UNION
    SELECT :ParaCCenter FROM dual))
    The result is
    (SELECT id FROM ccenters
    ERROR at line 3:
    ORA-01427: single-row subquery returns more than one row
    How this query can be rewritten for the given functionality. Any response will be highly appreciated.
    Thanks

    And if you want to use DECODE:
    SQL> BEGIN
      2  :paraccenter:=0;
      3  END;
      4  /
    PL/SQL procedure successfully completed.
    SQL> select  *
      2    from  ccenters
      3    where :paraccenter = decode(:paraccenter,0,0,id)
      4  /
            ID NAME                       BASE
             1 NUST
             2 SEECS                         1
             3 NBS                           1
    SQL> BEGIN
      2  :paraccenter:=2;
      3  END;
      4  /
    PL/SQL procedure successfully completed.
    SQL> select  *
      2    from  ccenters
      3    where :paraccenter = decode(:paraccenter,0,0,id)
      4  /
            ID NAME                       BASE
             2 SEECS                         1
    SQL> SY.

Maybe you are looking for

  • The new iPhone 6 and iPhone 6 Plus DO have differences

    I am considering entering the smart phone market, mainly for a portable camera to take on trips.  I have narrowed it down to the leading Samsung Galaxy S5 and one of the new iPhones - 6 or 6 Plus.  I spoke with an adviser on Live Chat and asked if th

  • How to auto adjust layout in bsp page

    hi i have created a bsp page , in diffrent resolution screen its displaying siffrent every time is there any techniue by which my layout autoadjust with any resolution. thanks

  • Prod Order Type Automatic selection

    Dear Sir, In MTO, after MRP run, during the conversion of planned order to production order, how system detect the production order type automatically if I have more that one production order type. Thanks and Regards, Chetan

  • SAP Interactive Forms by Adobe on a Windows Mobile device

    Is it possible to use SAP Interactive Forms by Adobe on a windows mobile device in terms of completing a form offline and submitting it when connected to the network again?

  • Workflow for PO re- release

    Gurus, ply help in develpoing a workflow Po re-released means value changed from 10rs to 20 rs an alert to the manager how is this possible regards amey