Date functions in WHERE clause? HELP

The following two queries are identical except for how I supply the date values in the where clause, yet the first query using the a custom my_date function runs 30x slower than the one using the TO_DATE() function. Both return DATE types...any reason for the difference?
SELECT * from outcomes
WHERE start_time >=fn_my_date('START_MONTH')
AND start_time < fn_my_date('END_MONTH')+1
-- This runs 30x faster--
SELECT * from outcomes
WHERE start_time >=TO_DATE('08/01/2001','MM/DD/YYYY')
AND start_time < TO_DATE('08/31/2001','MM/DD/YYYY')+1
On the flip side, I've also experienced queries running slower using the TO_DATE() function vs the LAST_DAY(sysdate) for equivalenet dates.
null

I haven't seen the message coming up when using LENGTH or SUBSTR, but every time I connect to a database or attempt to change my preferences (including the "NLS Parameters: Comp" preference), this appears.
You are attempting to set the preference you should be to switch this from ANSI to something else, but SQL Developer is ignoring the preference setting (which I think is a bug).
A way to set the NLS_COMP to something else is to use something like "alter session set nls_comp = BINARY;". Note that changing any preference after that appears to overwrite this and you need to do this for each new connection you start.

Similar Messages

  • Help:alternate for calling function in where clause

    Hi ,
    In below query i'm calling function in where clause to avoid COMPLETE status records,due to this query taking 700 secs to return result.If i'm remove below function condition it's returning results with in 5 secs.Can you some one advice to any alternate idea for this?
    WHERE mark_status != 'COMPLETE'
    SELECT assessment_school,
      subject,
      subject_option,
      lvl,
      component,mark_status,
      mark_status
      NULL AS grade_status,
      NULL AS sample_status,
      :v_year,
      :v_month,
      :v_formated_date,
      :v_type,
      cand_lang
    FROM
      (SELECT assessment_school,
        subject,
        subject_option,
        lvl,
        programme,
        component,
        paper_code,
        cand_lang,
        mark_entry.get_ia_entry_status(:v_year, :v_month, assessment_school, subject_option, lvl, cand_lang, component, paper_code) AS mark_status
      FROM
        (SELECT DISTINCT ccr.assessment_school,
          ccr.subject,
          ccr.subject_option,
          ccr.lvl,
          ccr.programme,
          ccr.language AS cand_lang,
          ccr.paper_code,
          ccr.component
        FROM candidate_component_reg ccr
        WHERE ccr.split_session_year = :v_year
        AND ccr.split_session_month  = :v_month
        AND EXISTS
          (SELECT 1
          FROM IBIS.subject_component sc
          WHERE sc.year          = ccr.split_session_year
          AND sc.month           = ccr.split_session_month
          AND sc.paper_code      = ccr.paper_code
          AND sc.assessment_type = 'INTERNAL'
          AND sc.subject_option NOT LIKE '%self taught%'
          AND sc.component NOT IN ('PERFORMANCE  PRODUCTION','PRESENTATION WORK','REFLECTIVE PROJECT','SPECIAL SYLLABUS INT. ASSESSMENT')
        AND NVL(ccr.withdrawn,'N') = 'N'
        AND ccr.mark_status       != 'COMPLETE'
        AND EXISTS
          (SELECT 1
          FROM school s
          WHERE s.school_code   = ccr.assessment_school
          AND s.training_school = 'N'
    WHERE mark_status != 'COMPLETE';

    One thing you can test quickly is to put the function call in it's own select ...from dual.
    This might make a difference.
    However, only you can check this, I don't have your tables or data.
    So, what happens if you use:
        paper_code,
        cand_lang,
      (select mark_entry.get_ia_entry_status(:v_year, :v_month, assessment_school, subject_option, lvl, cand_lang, component, paper_code) from dual ) AS mark_status
      FROM
        (SELECT DISTINCT ccr.assessment_school,  --<< is the DISTINCT really needed?
          ccr.subject,
          ccr.subject_option,
    ...Also, try to find out the purpose of that above DISTINCT, is it really needed or is there some join missing?

  • My function in where clause? help me please.

    hello friends, I need to call my function in where clause as dummy example below:
    declare
    name_ table1.name%type;
    function return_id(id number) return number is
    begin
    return 1;
    end return_id;
    begin
    select name into name_
    from table1
    where id = return_id(table1.id);
    end;
    raise exception: 'the function doesn't used in where clause'. why????

    -- CREATING A FUNCTION AVAILABLE THROUGH A PACKAGE SPEC.
    SQL> ed
    Wrote file afiedt.buf
      1  create package mypackage is
      2    function myfunc(p_val NUMBER) RETURN NUMBER;
      3* end;
    SQL> /
    Package created.
    SQL> ed
    Wrote file afiedt.buf
      1  create or replace package body mypackage is
      2    function myfunc(p_val NUMBER) RETURN NUMBER IS
      3    begin
      4      RETURN (p_val*p_val)-1;
      5    end;
      6* end;
    SQL> /
    Package body created.
    SQL> declare
      2    v_myval NUMBER := 2;
      3  begin
      4    select mypackage.myfunc(v_myval)
      5    into   v_myval
      6    from   dual;
      7    dbms_output.put_line(v_myval);
      8    select mypackage.myfunc(v_myval)
      9    into   v_myval
    10    from   dual;
    11    dbms_output.put_line(v_myval);
    12  end;
    13  /
    3
    8
    PL/SQL procedure successfully completed.
    -- CREATING A STANDALONE DATABASE FUNCTION.
    SQL> create or replace function myfunc2(p_val number) return number is
      2  begin
      3    return (p_val*2)+1;
      4  end;
      5  /
    Function created.
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    v_myval NUMBER := 2;
      3  begin
      4    select myfunc2(v_myval)
      5    into    v_myval
      6    from dual;
      7    dbms_output.put_line(v_myval);
      8    select myfunc2(v_myval)
      9    into    v_myval
    10    from dual;
    11    dbms_output.put_line(v_myval);
    12* end;
    SQL> /
    5
    11
    PL/SQL procedure successfully completed.
    -- CREATING A LOCAL FUNCTION IN THE ANONYMOUS PL/SQL BLOCK
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    v_myval NUMBER := 2;
      3    function myfunc3(p_val number) return number is
      4    begin
      5      return (p_val*p_val*p_val);
      6    end;
      7  begin
      8    select myfunc3(v_myval)
      9    into    v_myval
    10    from dual;
    11    dbms_output.put_line(v_myval);
    12    select myfunc3(v_myval)
    13    into    v_myval
    14    from dual;
    15    dbms_output.put_line(v_myval);
    16* end;
    SQL> /
      select myfunc3(v_myval)
    ERROR at line 8:
    ORA-06550: line 8, column 10:
    PLS-00231: function 'MYFUNC3' may not be used in SQL
    ORA-06550: line 8, column 10:
    PL/SQL: ORA-00904: : invalid identifier
    ORA-06550: line 8, column 3:
    PL/SQL: SQL Statement ignored
    ORA-06550: line 12, column 10:
    PLS-00231: function 'MYFUNC3' may not be used in SQL
    ORA-06550: line 12, column 10:
    PL/SQL: ORA-00904: : invalid identifier
    ORA-06550: line 12, column 3:
    PL/SQL: SQL Statement ignored
    SQL>As you can see (and as previously mentioned by someone else) the function has to be available to the SQL engine either via a datavbase package or as a database function. If you declare the function locally within the anonymous PL/SQL block then the function only exists in the scope of that PL/SQL block and thus when your SQL statement is sent to the SQL engine for execution, the SQL engine can't see it.
    ;)

  • Performance with dates in the where clause

    Performance with dates in the where clause
    CREATE TABLE TEST_DATA
    FNUMBER NUMBER,
    FSTRING VARCHAR2(4000 BYTE),
    FDATE DATE
    create index t_indx on test_data(fdata);
    query 1: select count(*) from TEST_DATA where trunc(fdate) = trunc(sysdate);
    query 2: select count(*) from TEST_DATA where fdate between trunc(sysdate) and trunc(SYSDATE) + .99999;
    query 3: select count(*) from TEST_DATA where fdate between to_date('21-APR-10', 'dd-MON-yy') and to_date('21-APR-10 23:59:59', 'DD-MON-YY hh24:mi:ss');
    My questions:
    1) Why isn't the index t_indx used in Execution plan 1?
    2) From the execution plan, I see that query 2 & 3 is better than query 1. I do not see any difference between execution plan 2 & 3. Which one is better?
    3) I read somewhere - "Always check the Access Predicates and Filter Predicates of Explain Plan carefully to determine which columns are contributing to a Range Scan and which columns are merely filtering the returned rows. Be sceptical if the same clause is shown in both."
    Is that true for Execution plan 2 & 3?
    3) Could some one explain what the filter & access predicate mean here?
    Thanks in advance.
    Execution Plan 1:
    SQL> select count(*) from TEST_DATA where trunc(fdate) = trunc(sysdate);
    COUNT(*)
    283
    Execution Plan
    Plan hash value: 1486387033
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 9 | 517 (20)| 00:00:07 |
    | 1 | SORT AGGREGATE | | 1 | 9 | | |
    |* 2 | TABLE ACCESS FULL| TEST_DATA | 341 | 3069 | 517 (20)| 00:00:07 |
    Predicate Information (identified by operation id):
    2 - filter(TRUNC(INTERNAL_FUNCTION("FDATE"))=TRUNC(SYSDATE@!))
    Note
    - dynamic sampling used for this statement
    Statistics
    4 recursive calls
    0 db block gets
    1610 consistent gets
    0 physical reads
    0 redo size
    412 bytes sent via SQL*Net to client
    380 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    1 rows processed
    Execution Plan 2:
    SQL> select count(*) from TEST_DATA where fdate between trunc(sysdate) and trunc(SYSDATE) + .99999;
    COUNT(*)
    283
    Execution Plan
    Plan hash value: 1687886199
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 9 | 3 (0)| 00:00:01 |
    | 1 | SORT AGGREGATE | | 1 | 9 | | |
    |* 2 | FILTER | | | | | |
    |* 3 | INDEX RANGE SCAN| T_INDX | 283 | 2547 | 3 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    2 - filter(TRUNC(SYSDATE@!)<=TRUNC(SYSDATE@!)+.9999884259259259259259
    259259259259259259)
    3 - access("FDATE">=TRUNC(SYSDATE@!) AND
    "FDATE"<=TRUNC(SYSDATE@!)+.999988425925925925925925925925925925925
    9)
    Note
    - dynamic sampling used for this statement
    Statistics
    7 recursive calls
    0 db block gets
    76 consistent gets
    0 physical reads
    0 redo size
    412 bytes sent via SQL*Net to client
    380 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    1 rows
    Execution Plan 3:
    SQL> select count(*) from TEST_DATA where fdate between to_date('21-APR-10', 'dd-MON-yy') and to_dat
    e('21-APR-10 23:59:59', 'DD-MON-YY hh24:mi:ss');
    COUNT(*)
    283
    Execution Plan
    Plan hash value: 1687886199
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 9 | 3 (0)| 00:00:01 |
    | 1 | SORT AGGREGATE | | 1 | 9 | | |
    |* 2 | FILTER | | | | | |
    |* 3 | INDEX RANGE SCAN| T_INDX | 283 | 2547 | 3 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    2 - filter(TO_DATE('21-APR-10','dd-MON-yy')<=TO_DATE('21-APR-10
    23:59:59','DD-MON-YY hh24:mi:ss'))
    3 - access("FDATE">=TO_DATE('21-APR-10','dd-MON-yy') AND
    "FDATE"<=TO_DATE('21-APR-10 23:59:59','DD-MON-YY hh24:mi:ss'))
    Note
    - dynamic sampling used for this statement
    Statistics
    7 recursive calls
    0 db block gets
    76 consistent gets
    0 physical reads
    0 redo size
    412 bytes sent via SQL*Net to client
    380 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    1 rows processed

    Hi,
    user10541890 wrote:
    Performance with dates in the where clause
    CREATE TABLE TEST_DATA
    FNUMBER NUMBER,
    FSTRING VARCHAR2(4000 BYTE),
    FDATE DATE
    create index t_indx on test_data(fdata);Did you mean fdat<b>e</b> (ending in e)?
    Be careful; post the code you're actually running.
    query 1: select count(*) from TEST_DATA where trunc(fdate) = trunc(sysdate);
    query 2: select count(*) from TEST_DATA where fdate between trunc(sysdate) and trunc(SYSDATE) + .99999;
    query 3: select count(*) from TEST_DATA where fdate between to_date('21-APR-10', 'dd-MON-yy') and to_date('21-APR-10 23:59:59', 'DD-MON-YY hh24:mi:ss');
    My questions:
    1) Why isn't the index t_indx used in Execution plan 1?To use an index, the indexed column must stand alone as one of the operands. If you had a function-based index on TRUNC (fdate), then it might be used in Query 1, because the left operand of = is TRUNC (fdate).
    2) From the execution plan, I see that query 2 & 3 is better than query 1. I do not see any difference between execution plan 2 & 3. Which one is better?That depends on what you mean by "better".
    If "better" means faster, you've already shown that one is about as good as the other.
    Queries 2 and 3 are doing different things. Assuming the table stays the same, Query 2 may give different results every day, but the results of Query 3 will never change.
    For clarity, I prefer:
    WHERE     fdate >= TRUNC (SYSDATE)
    AND     fdate <  TRUNC (SYSDATE) + 1(or replace SYSDATE with a TO_DATE expression, depending on the requirements).
    3) I read somewhere - "Always check the Access Predicates and Filter Predicates of Explain Plan carefully to determine which columns are contributing to a Range Scan and which columns are merely filtering the returned rows. Be sceptical if the same clause is shown in both."
    Is that true for Execution plan 2 & 3?
    3) Could some one explain what the filter & access predicate mean here?Sorry, I can't.

  • Can't have aggregate function in WHERE clause clause

    Dear all,
    I've created object in BO XI 3.1 Designer with following criterias:
    http://img4.imageshack.us/img4/833/20111201124314.th.jpg
    It is a simple number - 1,2,3,4,5.
    Now I need to use this object as a criteria for WHERE function in another object.
    http://img607.imageshack.us/img607/1543/20111201124717.th.jpg
    I receive an error "Can't have aggregate function in WHERE clause <clause>"
    How can I overcome this?
    P.S. I'm sorry in advance if such topic already exist - I didn't found one.
    Edited by: Ashot Antonyan on Dec 1, 2011 9:50 AM
    Edited by: Ashot Antonyan on Dec 1, 2011 9:51 AM

    Hi,
    You will have to use Sub query to achieve this. Give more details on what is available and what you need then i could help you out with the complete solution.
    Thanks,
    Ravichandra K

  • Using round off function in where clause

    Hi All,
    I'm trying to use round off function in where clause, I seek help in completing this script.
    WITH CR_Details AS
    (Select
    request_id,
    parent_request_id,
    fcpt.user_concurrent_program_name Request_Name, phase_code, status_code,
    round((fcr.actual_completion_date - fcr.actual_start_date),3) * 24 * 60 as Run_Time,
    round(avg(round(to_number(actual_start_date - fcr.requested_start_date),3) * 1440),2) wait_time,
    fu.User_Name Requestor,
    fcr.argument_text parameters,
    to_char (fcr.requested_start_date, 'MM/DD HH24:mi:SS') requested_start,
    to_char(actual_start_date, 'MM/DD/YY HH24:mi:SS') ACT_START,
    to_char(actual_completion_date, 'MM/DD/YY HH24:mi:SS') ACT_COMP,
    fcr.completion_text
    From
    apps.fnd_concurrent_requests fcr,
    apps.fnd_concurrent_programs fcp,
    apps.fnd_concurrent_programs_tl fcpt,
    apps.fnd_user fu
    Where 1=1
    and fcr.concurrent_program_id = fcp.concurrent_program_id
    and fcp.concurrent_program_id = fcpt.concurrent_program_id
    and fcr.program_application_id = fcp.application_id
    and fcp.application_id = fcpt.application_id
    and fcr.requested_by = fu.user_id
    and fcpt.language = 'US'
    and fcr.actual_start_date like sysdate )
         select crd.*
         from CR_Details crd
         where Run_time <> '0'
         AND wait_time <> '0'
    GROUP BY
    crd.request_id,
    crd.parent_request_id,
    crd.fcpt.user_concurrent_program_name,
    crd.requested_start_date,
    crd.User_Name,
    crd.argument_text,
    crd.actual_completion_date,
    crd.actual_start_date,
    crd.phase_code,
    crd.status_code,
    crd.resubmit_interval,
    crd.completion_text,
    crd.resubmit_interval,
    crd.resubmit_interval_unit_code,
    crd.description
    Not sure about the GROUPBY function referencing the "crd." .

    Hi,
    The best thing for you to do is start over. Start as small as possible, then take baby steps.
    Pick one of the tables; fcr perhaps, and write a query that just uses that table, like this:
    SELECT    *
    FROM       apps.fnd_concurrent_requests     fcr
    WHERE       fcr.actual_start_date          >= TRUNC (SYSDATE)
    AND       fcr.actual_start_dt          <  TRUNC (SYSDATE) + 1
    ;(I think this is what you meant to do when you said "... LIKE SYSDATE".)
    Make sure this little query gets credible results. When that tiny query is working perfectly, add another line or two. You can cut and paste code from what you posted, if that helps you.
    If you get stuck, post the last version of your code that worked perfectly, and the latest version (only a line or two bigger) that has the problem. Describe what the problem is. If you get an error, post the complete error message. In any event, post CREATE TABLE and INSERT statements for all the tables and columns needed to run the query, and the results you want to get from that query.
    When you post any code, format it, so that how the code looks on the screen gives some clues about how it is working.
    When you post any formatted text on this site, type these 6 characters:
    \(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
    If you going to use the AVG function in the sub-query, then you probably need to do a GROUP BY in the sub-query.
    If you're not using any aggregate functions (like AVG) in the main query, then you probably don't want to do a GROUP BY in the main query.
    I know this is a lot of work.  I'm sorry.  If there was an easier way, I wouldn't ask you to do all this.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

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

  • Clarification on using function in where clause of oracle sql query

    I have an issue in regarding function using where clause of sql query..
    We are facing performance issue while executing query, so in what ways to improve the performance of the query which i have posted below.
    select col ,case when my_function(parameter)
    from tab1 a ,tab2 b,tabl3 c
    where a.column1=b.column2
    and b.column3 =c.column6
    and my_function(parameter)>0
    Regards
    Dinesh
    Edited by: wild fire on May 18, 2012 4:15 PM

    Dinesh,
    remind that when you use a function in the where clause it normally will get started for each record in the table.
    So your answer is two-fold:
    1. make the function only start when needed by adding a function based index on the table (this will make inserts and updates slower)
    2. make the function faster by adding the DETERMINISTIC clause if possible. this will make Oracle "cache" the result of the function.
    Regards,
    Richard
    blog: http://blog.warp11.nl
    twitter: @rhjmartens
    If this question is answered, please mark the thread as closed and assign points where earned..

  • Using function in where clause

    I have created a function as follows
    create or replace FUNCTION get_codes RETURN varchar2 IS
    scodes varchar2(50) := 'A1,A2';
    BEGIN
    scodes := '('''||REPLACE(scodes,',',''',''')||''')';
    return scodes;
    END;
    this function returns ('A1','A2')
    now i want to use this in where clause, both below statements fetches no rows
    select * from tablea where code in (select get_codes from dual);
    select * from tablea where code in get_codes;
    but the following will fetch rows
    select * from tablea where code in ('A1','A2')
    how to use function in where clause
    Thanks

    Hi,
    The code that works:
    where code in ('A1','A2')is testing whther code is either of two 2-character strings, A1 or A2.
    The ones that don't work, such as:
    where code in get_codes;are comparing code to one 9-character string: 'A1','A2', where characters 1, 4, 6 and 9 are single-quotes. (The function can only return one value.)
    You can use dynamic SQL to embed that 9-character string in part of your query, where it will be interpreted as two 2-character strrings.
    [Oracle Base|http://www.oracle-base.com/articles/misc/DynamicInLists.php] and AskTom have good pages on the subject of dynamic IN-lists.
    Of you can use INSTR or LIKE:
    where INSTR ( get_codes
                , '''' || code || ''''
                ) > 0

  • Case function in where clause

    i insert the below rows in a table cash_book.
    PRJ_CODE PRJ_NAME
    1203 SHIFA
    1203 SHIFA
    1203 SHIFA
    1202 FATIMA
    1202 FATIMA
    1203 SHIFA
    if i select 1202 code then return 1202 code rows, if i select 1203 then returns 1203 code rows, if i select other than both values then returns all rows. how can i define in one query with use (case or decode function) in where clause ??

    perhaps something like this:
    create table test
    as
    select 1200 + rownum prj_code
      from dual
    connect by level <= 10;
    select * from test;
      PRJ_CODE
          1201
          1202
          1203
          1204
          1205
          1206
          1207
          1208
          1209
          1210
    var v_PRJ_CODE number
    exec :v_PRJ_CODE := 1200;
    select *
      from test
    where PRJ_CODE = :v_PRJ_CODE
        or 1 = case when :v_PRJ_CODE in (1202, 1203) then 0 else 1 end;
      PRJ_CODE
          1201
          1202
          1203
          1204
          1205
          1206
          1207
          1208
          1209
          1210
    exec :v_PRJ_CODE := 1203;
    select *
      from test
    where PRJ_CODE = :v_PRJ_CODE
        or 1 = case when :v_PRJ_CODE in (1202, 1203) then 0 else 1 end;
      PRJ_CODE
          1203Regards
    Martin

  • Using user defined function in where clause

    Hi,
    I have defined function to get maximum date before passed date on the table 'A' and I'm using the same function to get details on that date from the same table 'A' in where clause.
    for ex:
    SELECT x,y,z
    FROM A
    WHREE a.date = max_date;
    But on one database instance it is running fine and on other it is going in a infinite loop.
    Pls help me out
    Thanks in advance,
    Prashant

    Hello Siva,
    sorry, but I don't understand your reply:
    This is not right forum to posting this question.
    You are from which module or working any 3rd party application.
    MaxDB 7.7.07.16 is the community version of MaxDb,
    we are not using it for SAP
    and no 3rd party software is required to reproduce my problem,
    Sql Studio or Database Studio will do.
    Regards,
    Silke Arnswald

  • Call of constant function in where clause

    Hi there,
    I want to execute the following query
    select a_field
    from a_table
    where another_field = a_func('10')
    where a_func is a stored function and returns a value dependent on its argument. Same argument returns same result. How to see, the argument is a_table-independent (constant), so the result is a constant. How can I prevent oracle from executing a_func for each row of a_table? I know, that the following code works:
    select a_field
    from a_table
    where another_field = (select a_func('10') from dual)
    but this less readable and harder to explain to low-level-users in my eyes. Is there any pragma or anything else I can do, so the first statement can executed with a single(!) execution of a_func?
    Thanks, ALu.
    null

    Unfortunately Variables do not help in this case. The query-text goes (in one of three uses) into a Delphi-TQuery-Object and after opening it the results are shown. Declaring Variables is not allowed this way. I want a single (very) easy(!) statement to be executed (see the first one above). To explain the use of the function is ok (instead of using any subselect this function calculates), but any subselect- or variable-usage is beyond most of my app-users that want access to the stored data. The solution must be found in declaration of a_func (to put it into a package an restrict it by WNPS, WNDS also did not help ...)
    Thanx anyway, ALu.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Narayan Acharya ([email protected]):
    How about defining a variable, assigning it a value by calling the function, then using the variable in the where clause ?
    Trust that would work ?
    Narayan.<HR></BLOCKQUOTE>
    null

  • Calling function in where clause

    <BR>
    Hi
    I receive a string of form '333,444,55R5,B342,A556' as string value which should be passed to condition check to filter records in where clause of query.
    to make string form in right format with proper quotes separated i have written one function which does that job
    CREATE OR REPLACE FUNCTION DW.F_TASK(P_VAL VARCHAR2) RETURN VARCHAR2 AS
    V_VAL VARCHAR2(4000);
    V_VAL1 VARCHAR2(4000);
    BEGIN
    V_VAL1:=REPLACE(P_VAL,',',''',''');
    V_VAL:='('||''''||V_VAL1||''''||')';
    DBMS_OUTPUT.PUT_LINE(V_VAL);
    RETURN V_VAL;
    END F_TASK;
    When i fire below query it doesnt give me output
    SELECT * FROM CONTRACT_TABLE WHERE
    CONTRACT_NUMBER IN F_TASK('333,444,55R5,B342,A556');
    when i fire below query
    SELECT F_TASK('333,444,55R5,B342,A556') FROM DUAL
    below is the output
    ('333','444','55R5','B342','A556')
    note: above output holds quotes now.
    Issue: when i pass paratmeters directly to funciton then i could see output with brackets but when i use same funciton with parameters iin query i dont see data afer firing the query.. though i have those contract numbers in the table.
    Please help how to acquire this
    Thanks,
    Naren
    </BR>

    You haven't described your problem clearly (see: {message:id=9360002})
    However, it sounds like you want a varying IN list, which is also in the FAQ: {message:id=9360009}

  • How to use the MAX DATE condition in WHERE CLAUSE FILEDS

    Hi,
    I am trying to fetch the result for getting maximun date but when i try to execute the query i am getting the error as follows.
    CONDITION : trunc(max(RD.DATERECEIVED)) BETWEEN TO_DATE('01/08/2011','DD/MM/YYYY') AND TO_DATE('01/08/2011','DD/MM/YYYY')
    ERROR: Group function is not allowed here.
    CHEERS,
    PRABU AMMAIAPPAN

    I see a couple of problems here.
    First, what you posted below is not a syntactically valid query. It seems to be part of a larger query, specifically, this looks to be only the GROUP BY clause of a query.
    Prabu ammaiappan wrote:
    Hi,
    I Have a group function in the Query. Below is the Query i have used it,
    GROUP BY S.FREIGHTCLASS,
    R.CONTAINERKEY,
    S.SKU,
    S.DESCR ||S.DESCRIPTION2,
    S.PVTYPE,
    RD.LOTTABLE06,
    R.WAREHOUSEREFERENCE,
    RD.TOLOC,
    R.ADDWHO,
    R.TYPE,
    S.CWFLAG,
    S.STDNETWGT,
    S.ORDERUOM,
    R.ADDDATE,
    C.DESCRIPTION,
    (CASE WHEN P.POKEY LIKE '%PUR%' THEN 'NULL' ELSE to_char(P.PODATE,'dd/mm/yyyy') END),
    NVL((CASE WHEN R.ADDWHO='BOOMI' THEN RDD.SUPPLIERNAME END),SS.COMPANY),
    RDD.BRAND,
    S.NAPA,
    RD.RECEIPTKEY,
    R.SUSR4,
    P.POKEY,
    RDD.SUSR1,
    r.STATUS, DECODE(RDD.SUSR2,' ',0,'',0,RDD.SUSR2),
    rd.SUSR3Second, the answer to your primary question, "How do I add a predicate with with a MAX() function to my where clause?" is that you don't. As you discovered, if you attempt to do so, you'll find it doesn't work. If you stop and think about how SQL is processed, it should make sense to you why the SQL is not valid.
    If you want to apply a filter condition such as:
    trunc(max(RD.DATERECEIVED)) BETWEEN TO_DATE('01/08/2011','DD/MM/YYYY') AND TO_DATE('01/08/2011','DD/MM/YYYY')you should do it in a HAVING clause, not a where clause:
    select ....
      from ....
    where ....
    group by ....
    having max(some_date) between this_date and that_date;Hope that helps,
    -Mark

  • Function in where clause is executed and returns null instead a valid value

    Hi all
    i have a strange problem.
    There is a access-application where i have a pass-through-sql.
    This sql is fetching a foreign exchange rate from a history table which holds this rates for every day and i'm going to fetch the rate for the last day of the last month.
    Example of the fraction which does not work and which i executed directly on the DB, Oracle 11.2.0.3.0:
    select val
    ,last_day(add_months((trunc(sysdate)+3),-1)) tag
    from k.obj_ts_hist th
    where obj_id = 2660
    and eff_date = last_day(add_months((trunc(sysdate)+3),-1))
    --and      eff_date = '01-mar-2013'
    eff_date on table k.obj_ts_hist is defined as date.
    The function last_day in the where clause leads to null for both values in the select (val and tag).
    If i use eff_date = '01-mar-2013' it's all ok, i get the rigth values.
    I tested some combinations of the clause, e.g.
    eff_date = to_date(to_char(last_day(add_months((trunc(sysdate)+3),-1)),'YYYYMMDD'),'YYYYMMDD')
    or
    to_char(eff_date,'YYYYMMDD') = to_char(last_day(add_months((trunc(sysdate)+3),-1)),'YYYYMMDD') , etc., but all of them do not work.
    Also i tested the clause with select ... from dual, e.g.
    select eff_date from
    select last_day(add_months((trunc(sysdate)+3),-1)) eff_date from dual
    where eff_date = last_day(add_months((trunc(sysdate)+3),-1))
    This work's !
    A cross check on Oracle 9.2.0.8.0: it works as expected !
    Thanks in advance for your effort.
    Regards, Konrad

    a little simulation of your formula
    select the_date,last_day(add_months((trunc(the_date) + 3),-1)) eff_date
      from (select to_date('20130224','yyyymmdd') + level - 1 the_date
              from dual
            connect by level <= 7
           )reveals you might have been querrying future rates
    THE_DATE   EFF_DATE 
    24.02.2013 31.01.2013
    25.02.2013 31.01.2013
    26.02.2013 28.02.2013  /* future date */
    27.02.2013 28.02.2013  /* future date */
    28.02.2013 28.02.2013  /* current date */
    01.03.2013 28.02.2013
    02.03.2013 28.02.2013Regards
    Etbin

Maybe you are looking for