Adding to WHERE based on an IF statement in a procedure

Is this legitimate?
select CRIME_CLASSIFICATION_ID, crime_type, nvl(count(CRIME_CLASSIFICATION_ID),0) as CRIMECNT
From vaps.vw_offenses
    where location_id in (vaLoc)
    If fromdate is not null Then
        AND offense_date >= to_date(fromdate, 'mm/dd/yyyy')  AND offense_date <= to_date(todate,'mm/dd/yyyy')
    End If;
group by crime_classification_id, crime_type
If it's not legitimate, how do I get the added clause into my query based on the value of fromdate?
Thanks
Chris

Hi, Chris,
Perhaps you want soemthing like this:
WHERE   location_id in (vaLoc)
AND     (    fromdate     IS NULL
        OR   offense_date BETWEEN TO_DATE (fromdate, 'mm/dd/yyyy')
                          AND     TO_DATE (todate,   'mm/dd/yyyy')
By the way, storing date information in string columns or variables is a really bad idea.  Use DATE columns and variables for date information; it will be simpler, more robust and more efficient; you won't need to use TO_DATE in queries like this.
I hope this answers your question.
If not, post  a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
Point out where the condition above is giving the wrong results, and explain, using specific examples, how you get the correct results from the given data in those places.  If you changed the condition at all, post your code.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002

Similar Messages

  • Where is "Display as Text (based on LOV, saves state)"

    I am developing an application using the version of HTMLDB that comes with OracleXE.
    I have a tabular form, in which the default column for a particular item is sourced from a previously entered item on another page. This is a lookup item and needs to be non-changeable by the user. So I want to use the "Display As" option "Display as Text (based on LOV, saves state)". However, this option is not available in the pull-down for "Display As". The option "Display as Text (based on LOV, does not save state)" is available, but using this option means that the item is not saved to the database.
    Does anyone know why the stateful display as option is not available.
    Thanks in advance,
    Rob

    Scott, thanks for the feedback. The problem is that although the value is displayed correctly, it is NOT saved to the database. Effectively, header information is entered in a previous screen, and one of the entered fields is written to each of the detail lines associated with the header. We are re-platforming an existing application (written in an obscure form of basic), and at this stage changing the schema is not an option. So this header item (which is a foreign key to a "store" table), is meant to be written down to each line, on the screen displayed as the store name, and written to the inserted lines as the lookup id. If I use the dispaly as text save state option teh database is updated correctly, but displays the id rather than name on the screen. The only display as lov option i can see is the one that does not save state. If I use this option the screen displays as I want but the database is not updated. I need to use the display as lov saves state option. but i do not see it.
    Any ideas?
    Thanks for your time.
    Rob

  • Annoying [ ] with "Display as Text (based on LOV, saves state)"

    Hi all,
    I've got a form where some items are shown as “Display as Text”. These values are displayed normally.
    But I also have some items which are shown as “Display as Text (based on LOV, saves state)”. When I run the page and these items don't return a value (because it isn't filled in in another page), we get the annoying []-sings.
    Is there anyone who knows how to fix this, I just want to display nothing when there is no value.

    I've tried this one already.
    I tried to leave it blank, & nbsp ; , text,... but the problem is that our lov is build by a function and it always returns a query, so the LOV is never null.
    I figured something out: made a process after header that has some cursors to get the values from the database and changed the item to a simple display as text-field. There were only 4 that were wrong, so it wasn't to much work.

  • Cannot see Display as Text (based on lov, saves state)

    I have raised this question on the HTMLDB forum (Where is "Display as Text (based on LOV, saves state)" but in case its XE specific I am posting here too.
    I have a tabular form in the XE version of HTMLDB, and wish to use the "Display as Text (based on LOV, saves state)" option. However, this option does is not available to me - are there some conditions necessary for this option to be displayed?
    Thanks, Rob

    Rob,
    I doubt it's an XE specific issue. I've asked for more information in the original thread.
    Sergio

  • Values from a Multi-Select in the where clause of a Select statement

    I have a web page that solicits query parameters from the user.
    The selections that the user makes will populate the WHERE clause of a Select statement.
    One of the controls on the page is a multi-select control.
    When this page posts, I would like to execute a Select statement wherein the selected values from this control appear in the .. Column IN ( <list here> ) portion of the WHERE clause.
    This is an extremely common scenario, but I cannot seem to locate a how-to or message thread that addresses this specific case.
    I have an idea that it may involve dynamic SQL or Execute Immediate, but cannot seem to pin down the answer.
    Any help would be greatly appreciated!

    anonymous - As illustrated here: Re: Search on a typed in list of values
    Scott

  • Restrict the items while adding to cart based on zipcode.

    Hi All,
    I have a requirement of restricting the items while adding to cart based on the zipcode .
    For ex . If I enter zipcode of 10014 ,then I shld able to only add 2 items .If i enter some other zipcode like 55344 then I shld able to only add 5 items .
    Please help me to implement this scenerio.
    Advance thanks

    You can add a property in catalog for "Product" item-descriptor to refer to "ZipCodeQuantity" item-descriptor to define the restriction. This way even if the quantity for a particular product( for a particular zip) changes, it becomes easy.
    zipcodeQuantity will have productid, zipcode, quantity allowed.
    Use a droplet to show only the allowed quantity for a particular product for a particular zip code in product detail page. This way you can restrict them from adding in ProductDetail page itself.
    Just an another way.
    -karthik

  • Display As Text (based on LOV, saves state)  "p_t03" was assigned by APEX

    Hi All,
    I have an ITEM
    Name: P36_INT
    Display As: Display As Text (based on LOV, saves state)
    Output in my HTML:
    [input type="hidden" name="p_t03" value="YES" />[span id="P36_INT">Interior[/span>
      'p_t03' was assigned by APEX
      In my Javascript I have to access it as the following to get the "YES" value:
      var lintObj = html_GetElement('p_t03');
      Originally it was 'p_t01' but it changed from 'p_t01' to 'p_t03' somehow.. out of my control.
      How do I assign an ID or assign my own name to the "hidden" item?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Scott, thanks for the feedback. The problem is that although the value is displayed correctly, it is NOT saved to the database. Effectively, header information is entered in a previous screen, and one of the entered fields is written to each of the detail lines associated with the header. We are re-platforming an existing application (written in an obscure form of basic), and at this stage changing the schema is not an option. So this header item (which is a foreign key to a "store" table), is meant to be written down to each line, on the screen displayed as the store name, and written to the inserted lines as the lookup id. If I use the dispaly as text save state option teh database is updated correctly, but displays the id rather than name on the screen. The only display as lov option i can see is the one that does not save state. If I use this option the screen displays as I want but the database is not updated. I need to use the display as lov saves state option. but i do not see it.
    Any ideas?
    Thanks for your time.
    Rob

  • Display as text (based on LOV saves state) : possible border ?

    Hi,
    Like mentioned in the subject, I have many Display as text (based on LOV saves state). I would like to have a frame or a border around this field eventhough it's read only, like a regular field. How can I do that ?
    thank you.

    Depending on your visual requirements, you could change the items to Text Fields (disabled or not) then simply make the Read-Only atttribute set to "always" or something that would equate to "always". It would look just like a text field (since it would in fact be one), but would be read-only.
    Sadly, this would require you to change all your items to the new type which would take time.

  • Where did we find connection statement interfaces?

    where did we find connection ,statement interfaces when we are working with type 4 driver?

    where did we find connection ,statement interfaces
    when we are working with type 4 driver?The interfaces are always present in rt.jar. :)

  • Dynamic SQL statement in a Procedure

    Hi,
    is it possible to use a variable in place of a column name in a sql statement inside a procedure. Or to create the whole statement as a sql statement and execute it (preferrably with parameters - rather than concatinating the values).
    Thanks for any help or direction
    Elliot

    Turns out you can do the following very nicely (dynamic sql with parameterized values)
    Declare
    id number(10,0);
    sql_stmt varchar2(300);
    fieldName varchar2(30);
    fieldValue varchar2(100);
    id := 30;
    fieldName := 'somecolumn';
    fieldValue := 'some value';
    sql_stmt := 'UPDATE myTable SET ' || fieldName || ' = :1 WHERE id = :2';
    EXECUTE IMMEDIATE sql_stmt USING fieldValue, id;

  • Update statement in a procedure

    update pol_notification a
    set obj_id = v_presv_client_id
    where a.obj_id=v_client_id and a.obj_type='client' ;
    update pol_notification a
    set obj_id = v_address_id
    where a.obj_id=v_address_id and a.obj_type='address' ;
    I am using these two update statements in one procedure but the tables are not being updated.
    Any reason for that. Am i using the right statements?

    Here is the procedure.....
    CREATE OR REPLACE PROCEDURE SP_LOAD_CLIDUPDATA( )
         BEGIN
    DECLARE v_client_id integer ;
    DECLARE v_presv_client_id integer ;
    DECLARE v_processed_client_id integer;
         DECLARE v_address_id integer;
    DECLARE V_NUMBER integer;
    DECLARE v_counter integer DEFAULT 0;
    DECLARE cur_update CURSOR FOR
    SELECT client_id,presv_client_id,nvl(processed_client_id,0),nvl(address_id,0)
    FROM cli_dup_data ;
    DECLARE cur_num CURSOR FOR
    SELECT count(*) FROM cli_dup_data ;
    open cur_num;
    fetch cur_num into V_NUMBER;
    close cur_num;
    OPEN cur_update;
    loop
    IF v_counter = V_NUMBER THEN
    end loop;
    END IF;
    FETCH cur_update INTO v_client_id,v_presv_client_id,v_processed_client_id,v_address_id;     
    if v_client_id = 0 then
    COMMIT; --Commit transactions at batch level only
    ELSE
    If v_processed_client_id <> v_client_id then -- client_id is already processed
                   update pol_notification a set obj_id = v_presv_client_id --Update client_id
         where a.obj_id=v_client_id and a.obj_type='client';
         update pol_notification a set obj_id = v_address_id -- Update address_id
         where a.obj_id=v_address_id and a.obj_type='address';
    update pol_notify_history b set obj_id=v_presv_client_id,
    client_seq_nbr_i = 1
    where b.obj_id = v_client_id and b.obj_type='client';
    update pol_notify_history b set obj_id = v_address_id
    where b.obj_id = v_client_id and b.obj_type = 'address';
    update pol_int_name set client_id = v_presv_client_id --Update client_id
         where client_id=v_client_id;
         update cli_dup_data set processed_client_id=v_client_id --Keep a record of updated client_id in processed_client_id
         where client_id=v_client_id;
    end if;
              END IF;
    set v_counter = v_counter+1;
    END loop ;
    CLOSE cur_update;

  • MS SQL Server 7 - Performance of Prepared Statements and Stored Procedures

    Hello All,
    Our team is currently tuning an application running on WL 5.1 SP 10 with a MS
    SQL Server 7 DB that it accesses via the WebLogic jConnect drivers. The application
    uses Prepared Statements for all types of database operations (selects, updates,
    inserts, etc.) and we have noticed that a great deal of the DB host's resources
    are consumed by the parsing of these statements. Our thought was to convert many
    of these Prepared Statements to Stored Procedures with the idea that the parsing
    overhead would be eliminated. In spite of all this, I have read that because
    of the way that the jConnect drivers are implemented for MS SQL Server, Prepared
    Statments are actually SLOWER than straight SQL because of the way that parameter
    values are converted. Does this also apply to Stored Procedures??? If anyone
    can give me an answer, it would be greatly appreciated.
    Thanks in advance!

    Joseph Weinstein <[email protected]> wrote:
    >
    >
    Matt wrote:
    Hello All,
    Our team is currently tuning an application running on WL 5.1 SP 10with a MS
    SQL Server 7 DB that it accesses via the WebLogic jConnect drivers.The application
    uses Prepared Statements for all types of database operations (selects,updates,
    inserts, etc.) and we have noticed that a great deal of the DB host'sresources
    are consumed by the parsing of these statements. Our thought was toconvert many
    of these Prepared Statements to Stored Procedures with the idea thatthe parsing
    overhead would be eliminated. In spite of all this, I have read thatbecause
    of the way that the jConnect drivers are implemented for MS SQL Server,Prepared
    Statments are actually SLOWER than straight SQL because of the waythat parameter
    values are converted. Does this also apply to Stored Procedures???If anyone
    can give me an answer, it would be greatly appreciated.
    Thanks in advance!Hi. Stored procedures may help, but you can also try MS's new free type-4
    driver,
    which does use DBMS optimizations to make PreparedStatements run faster.
    Joe
    Thanks Joe! I also wanted to know if setting the statement cache (assuming that
    this feature is available in WL 5.1 SP 10) will give a boost for both Prepared Statements
    and stored procs called via Callable Statements. Pretty much all of the Prepared
    Statements that we are replacing are executed from entity bean transactions.
    Thanks again

  • SAP HANA - How to run alter table statement in HANA procedure?

    I am trying to run alter table statement in a procedure. HANA gives error saying
    SAP DBTech JDBC: [257] (at 1338): sql syntax error: ALTER TABLE is not allowed in SQLScript: line 36 col 8 (at pos 1338)
    How to run alter table statements in procedure?
    Thanks,
    Suren.

    Hi Rich Heilman,
    Thanks for your response.  I have tried with dynamic SQL. I am trying to add partitions to a non portioned table.
    EXECUTE IMMEDIATE 'ALTER TABLE ' || :SCHEMA_NAME || '.TARGET_TABLE PARTITION BY RANGE (TARGET_TYPE_ID) (PARTITION VALUE = 1, PARTITION VALUE = 2, PARTITION VALUE = 3, PARTITION VALUE = 4, PARTITION OTHERS)';
    Execution fails with error
    Could not execute 'CALL PARTITION_TARGET_TABLE('SUREN_TEST')' in 1.160 seconds .
    [129]: transaction rolled back by an internal error:  [129] "SUREN_TEST"."PARTITION_TARGET_TABLE": line 53 col 3 (at pos 2173): [129] (range 3)
    Any reasons for this error?
    Thanks,
    Suren.

  • Query slow down when added a where clause

    I have a procedure that has performance issue, so I copy some of the query and run in the sql plus and try to spot which join cause the problem, but I get a result which I can figuer out why. I have a query which like below:
    Select Count(a.ID) From TableA a
    -- INNER JOIN other tables
    WHERE a.TypeID = 2;
    TableA has 140000 records, when the where clause is not added, the count return quite quick, but if I add the where clause, then the query slow down and seems never return so I have to kill my SQL Plus session. TableA has index on TypeID and TypeID is a number type. When TablA has 3000 records, the procedure return very quick, but it slow down and hang there when the TableA contains 140000 records. Any idea why this will slow down the query?
    Also, the TypeID is a foreign key to another table (TableAType), so the query above can written as :
    Select Count(a.ID) From TableA a
    -- INNER JOIN other tables
    INNER JOIN TableAType atype ON a.TypeID = atype.ID
    WHERE atype.Name = 'typename';
    TableAType table is a small table only contains less than 100 records, in this case, would the second query be more efficient to the first query?
    Any suggestions are welcome, thanks in advance...
    Message was edited by:
    user500168

    TableA now has 230000 records and 28000 of them has the TypeID 2.
    I haven't use the hint yet but thank you for your reply which let me to to run a query to check how many records in TableA has TypeID 2. When I doing this, it seems pretty fast. So I begin with the select count for TableA only and gradually add table to join and seems the query is pretty fast as long as TableA is the fist table to select from.
    Before in my query TableA is the second table to join from, there is another table (which is large as well but not as large as TableA) before TableA. So I think this is why it runs slow before. I am not at work yesterday so the query given in my post is based on my roughly memory and I forget to mention another table is joined before TableA, really sorry about that.
    I think I learn a lesson here, the largest table need to be in the begining of the select statement...
    Thank you very much everyone.

  • How can define an outer join in the where clause of a flowr statement?

    Hi- In the sample below I'm joining two views based on username but in this case what I really want to use is an outer join instead. What is the syntax for that? I tried the (+) notation but that didn't seem to work..
    CREATE OR REPLACE PROCEDURE proc_ctsi_all is
    XMLdoc XMLType;
    BEGIN
    DBMS_XDB.deleteResource('/public/CTSI/ctsi_all_rpt1.xml',1);
    SELECT XMLQuery(
    '<Progress_Report>
    <Personnel_Roster>
    {for $c in ora:view("CTSI_INVEST_NONPHS_SOURCE_V"),
    $cphs in ora:view("CTSI_INVEST_PHS_SOURCE_V")
      let $username  := $c/ROW/COMMONS_USERNAME/text(),
    $expertise  := $c/ROW/AREA_OF_EXPERTISE/text(),
    $phsorg  := $cphs/ROW/PHS_ORGANIZATION/text(),
    $activitycode  := $cphs/ROW/ACTIVITY_CODE/text(),
    $username2  := $cphs/ROW/COMMONS_USERNAME/text()
    where $username eq $username2
         return
      <Investigator>
       <Commons_Username>{$username}</Commons_Username>
    <Area_of_Expertise>{$expertise}</Area_of_Expertise>
    <Federal_PHS_Funding>
    <Organization>{$phsorg}</Organization>
    <Activity_Code>{$activitycode}</Activity_Code>
    <Six_Digit_Grant_Number>{$grantnumber}</Six_Digit_Grant_Number>
    </Federal_PHS_Funding>
    </Investigator>}
    </Personnel_Roster>
    </Progress_Report>'
    RETURNING CONTENT) INTO XMLdoc FROM DUAL;
    IF(DBMS_XDB.CREATERESOURCE('/public/CTSI/ctsi_all_rpt1.xml', XMLdoc)) THEN
    DBMS_OUTPUT.PUT_LINE('Resource is created');
    ELSE
    DBMS_OUTPUT.PUT_LINE('Cannot create resource');
    END IF;
    COMMIT;
    END;
    /

    What you could do is use query within an XMLTable syntax. Via the COLUMNS parameter you then pass the "column" as XMLType to the following XMLtable statement.
    little bit like the following
    select hdjfdf
    from xmltable
       ({xquery}
        PASSING
        COLUMNS xmlfrag xmltype path 'xxx'
       ) a
    ,  xmltable
       ({the other stuff you need}
        PASSING a.xmlfrag
       ...etc
      ...etc                 I guess something simular can be done via XQuery straight away as well

Maybe you are looking for

  • Document split of a paymnt doc. for an invoice wit more than 999 line items

    Hi all, my question is . what steps should we take care of in the system, to make the the document Split automatically on a payment document when we post an Invoice with 1001 lines items in it. is it some thing in the config or do we need to update a

  • RFC Server with TCP / IP destination "Start"

    Hi all, I have a great problem, I have tried to look in the forum for the solution but I have not succeeded there. I repropose an user's very such to mine problems unfortunatelly without solution. I would to install a RFC Server on the client. From a

  • Hiding files and so on

    Hello, I'm wondering if it's possible to hide files, or make them invisible so that no one can accidentally delete them...

  • Oracle sql problem

    hello! PROBLEM 1: how do you drop foreign key constraint without constraint name? PROBLEM 2:i created two tables employee and department. CREATE TABLE EMPLOYEE FNAME VARCHAR (25) NOT NULL, MINIT VARCHAR(10), LNAME VARCHAR(25) NOT NULL, SSN CHAR(9), B

  • Reading and Writing to files in JARs

    I'm making a card game (TriPeaks). I need to store the scores for the users in files. Everything worked fine until I packaged it into a JAR. I tried using getClass().getResource(fileName); and then converting the URL to a URI to create the File objec