WHERE Clause sorting via String

Hi, I am joining 2 tables together while using DISTINCT in the Select statement but i need to sort all the rows that only match by Date '1996'. Problem is the conversion of the Date/Time value is not a String i need to return only the rows where companies
only placed orders in 1996.
I have also tried WHERE o.OrderDate LIKE '[1996]%'
Select DISTINCT CompanyName
From Customers c
JOIN Orders o ON o.OrderDate = c.CompanyName
WHERE o.OrderDate = '1996'
Order By CompanyName ASC;

I have to use DISTINCT & JOIN both tables it is apart of my assignment i have no choice in the matter.
I don't care what your professor tells you. I care that you learn SQL properly. And, hey, chances are good that I know T-SQL better than your professor.
DISTINCT is a highly abused keyword. To the extent that I would say that anytime you feel compelled to put in DISTINCT, you should start thinking if you might have gone wrong somewhere in the query. In a properly written join, there is rarely a need for
DISTINCT. If it happens, it may be one of the following:
1) Incomplete join-conditions. You joined two tables on a single column, when there is a two-column key linking them. That is, an error in the query.
2) The table that introduces duplicates in the query is not included in the SELECT list, because the table only appears in the query to check existence. This is your case, and you should use EXISTS instead (no matter what your professor asks you for).
3) You only include a subset of the columns from the tables in the query, and in those columns alone there is a duplicate. This may be a legit case, because you may only be looking for the distinct values. But you should review the requirements first.
I don't list these points because I am bored on a Saturday night. I list these points, because I see these errors over and over again by inexperienced programmers on the forums. And for that matter not-so-inexperienced programmers who once learnt bad
habits, and still are sticking to them.
So, OK, show your professor the query with JOIN and DISTINCT. But also show him the query with EXISTS. Also make sure that you understand how that query works. Your professor may or may not be impressed, but that does not matter. You learn for life, and
not to impress your professor.
And, oh, one more thing. The condition on OrderDate should really be:
 AND  O.OrderDate >= '19960101'
 AND  O.OrderDate < '19970101'
This is guaranteed to do an IndexSeek on OrderDate if the optimizer finds that this is the best solution. (Which it will not in this case, because you are looking at one third of all orders.) On the other hand, the condition that Ronen is trying to lure
you to use:
   WHERE datepart(year,o.OrderDate) = 1996
may or may not seek the index, depending on how the optimizer is implemented this year.
I realise that the talk about indexes and optimizer may go above your head at this point, but again, I am eager that you learn best practices early, and that you don't learn things you will need to unlearn later. You can remember this lesson in this
way: never entangle a column into an expression, if you can resolve the condition with simple comparison operations.
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • Where clause as variable string

    I'm building a where clause as a string based on submited
    form variables. As long as the form variable is an INTEGER the
    query works fine. If it is a charcter or character string I get the
    error: "Incorrect syntax near 'n' " where "n" was the content of
    the form variable.
    for example, when the form variable form.explain value is
    "n":
    <cfset w= " WHERE t_QUESTIONS.q_section =
    dbo.t_sections.sect_ID ">
    <cfif form.explain neq"">
    <cfset w = w & " and t_QUESTIONS.q_explain =
    '#form.explain#' " >
    </cfif>
    <cfquery name="q" datasource="mydsn">
    SELECT t_QUESTIONS.q_ID, t_QUESTIONS.q_section,
    t_QUESTIONS.q_explain,
    t_sections.sect_name, dbo.t_sections.sect_ID
    FROM dbo.t_QUESTIONS, dbo.t_sections
    #w#
    </cfquery>
    returns the error: [Macromedia][SQLServer JDBC
    Driver][SQLServer]Line 5: Incorrect syntax near 'n'.
    Outputting the var #w# content to the screen shows:
    WHERE t_QUESTIONS.q_section = dbo.t_sections.sect_ID and
    t_QUESTIONS.q_explain = 'n'
    which looks perfect, and the query displayed in the debugging
    screen is:
    SELECT t_QUESTIONS.q_ID, t_QUESTIONS.q_section,
    t_QUESTIONS.q_explain, t_sections.sect_name, dbo.t_sections.sect_ID
    FROM dbo.t_QUESTIONS, dbo.t_sections WHERE t_QUESTIONS.q_section =
    dbo.t_sections.sect_ID and t_QUESTIONS.q_explain = ''n''
    This has to be something simple, I just can't figure out what
    it is. I really need to get this to work, anybody have a clue about
    what's going on? (this is MX7)

    There is a function called PreserveSingleQuotes( ) that
    should solve your problem. Check out
    http://livedocs.macromedia.com/coldfusion/5.0/CFML_Reference/Functions184.htm#1110445

  • Where Clause - Modification via SDK

    Good morning,
    We are currently experiencing quite a large performance impact as our record selection formulas, and parameters are not hitting our database (in where clause) and therefore a large amount of data is being pulled back and then filtered out at the report level. Was hoping someone might be able to provide some information on how we could either dynamically append to the where clause via sdk, or devise optional workaround to make sure our selected parameters are passed to the db.
    Environment
    Solaris, with custom User Interface that passes information to crystal reports via SDK. Crystal Reports (2008) with Universe as datasource.
    Thanks,
    Ian S

    Good morning,
    We are currently experiencing quite a large performance impact as our record selection formulas, and parameters are not hitting our database (in where clause) and therefore a large amount of data is being pulled back and then filtered out at the report level. Was hoping someone might be able to provide some information on how we could either dynamically append to the where clause via sdk, or devise optional workaround to make sure our selected parameters are passed to the db.
    Environment
    Solaris, with custom User Interface that passes information to crystal reports via SDK. Crystal Reports (2008) with Universe as datasource.
    Thanks,
    Ian S

  • Where clause as a string in a table - how can i use...

    Table Name : WhereTable
    =======================
    QueryNo          WhereClause
    101          '&student.rno = 10 and &class.cid = 20'
    202          ..........
    I have some other tables and I joined all those to prepare a query.
    From the above query I get QueryNo.
    Based that query no. I need to fetch the where clause from the WhereTable.
    Then I need to use this where clause in this query itself to filter more.
    How can I do this.
    Please dont suggest PLSQL blocks for this, as I need to create a view with this query.
    It is something like this:
    select some_result from student, class, WhereTable
    where student.cid = class.cid and
    student.QueryNo = WhereTable.QueryNo and
    WhereTable.WhereClause

    Not sure about your actual requirements but here is something to play with:
    michaels>  create table wheretable (queryno, whereclause)
    as select 1, '&emp.empno = 7788 and &dept.deptno = 20' from dual
    Table created.
    michaels>  select t.* from wheretable, xmltable('.' passing dbms_xmlgen.getxmltype('select empno,emp.deptno,dname
                         from dept, emp, wheretable
                        where dept.deptno = emp.deptno and queryno = 1 and ' || replace(whereclause,chr(38))).extract('ROWSET/ROW')
                                             columns empno      integer path 'EMPNO',
                                                     deptno     integer path 'DEPTNO',
                                                     dname varchar2(10) path 'DNAME') t
         EMPNO     DEPTNO DNAME    
          7788         20 RESEARCH 

  • Where-clause for date-string

    Hi specialist's,
    i have a simple question about a select, but I think I do not see the forrest becaus of too many trees ...
    I have a table where timestamps are stored in seconds-since-epoch.
    I select a human readble format with this statement:
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog;
    TIMESTAMP
    19-10-2013 09:31:27
    19-10-2013 09:31:27
    19-10-2013 09:31:27
    Now I want to filter i.e. for dates of 19-01.... 
    My first try was this:
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog
      where timestamp like '19-%'
    Result:
    ORA-00904: "TIMESTAMP": invalid identifier
    My next try was this:
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog
    where 1 like '19%'
    Result: 0 rows!
    If I change this to
    ... where 1 like '%'
    I get all rows.
    What is wrong with my selects ???

    Felix_GG wrote:
    Hi specialist's,
    i have a simple question about a select, but I think I do not see the forrest becaus of too many trees ...
    I have a table where timestamps are stored in seconds-since-epoch.
    I select a human readble format with this statement:
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog;
    TIMESTAMP
    19-10-2013 09:31:27
    19-10-2013 09:31:27
    19-10-2013 09:31:27
    Now I want to filter i.e. for dates of 19-01....
    My first try was this:
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog
      where timestamp like '19-%'
    Result:
    ORA-00904: "TIMESTAMP": invalid identifier
    My next try was this:
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog
    where 1 like '19%'
    Result: 0 rows!
    If I change this to
    ... where 1 like '%'
    I get all rows.
    What is wrong with my selects ???
    Firstly, why not store timestamps as timestamps?
    Anyway, Your first select refers to the alias 'timestamp' which is only accessible in an outer query, not from within the where clause.
    Try
    Select * from (
    select to_char(DATE '1970-01-01' + TRXTIMESTAMP / (24*3600*1000), 'DD-MM-YYYY HH24:MI:SS')  as timestamp from txlog
    where timestamp like '19-%';
    But timestamp is not a good choice for an alias, as it is a keyword.
    Your second select is obvious - the number 1 will never be like '19%', but it will work
    in the third select: as 1 is indeed like '%' (anything).

  • Using multiple values in a where clause, for values only known at runtime

    Dear all
    I am creating a PL/SQL program which returns multiple rows of data but only where it meets a set id values that a user has previously chosen. The id values are stored in an associative array and are chosen by a user in the preceding procedure at run time.
    I know all the table and column names in advance. The only things I don't know are the exact number of ids selected from the id column and what their values will be. This will only be known at runtime. When the procedure is run by the user it prints multiple rows of data to a web browser.
    I have been reading the following posting, which I understand to a large extent, Query for multiple value search But I cannot seem to figure out how I would apply it to my work as I am dealing with multiple rows and a cursor.
    The code as I have currently written it is wrong because I get an error not found message in my web browser. I think the var_user_chosen_map_list_ids in the for cursor loop could be the problem. I am using the variable_user_chosen_map_list_ids to store all the id values from my associatative array as a string. Which I modified from the code that vidyadhars posted in the other thread.
    Should I be creating a OPEN FOR ref cursor and if so where would I put my associative array into it? At the moment I take the value, turning it into a string and IN part in the WHERE clause holds the string, allowing the WHERE clause to read all the values from it. I would expect the where clause to read everything in the string as 1 complete string of VARCHAR2 data but this would not be the case if this part of the code at least was correct. The code is as follows:
    --Global variable section contains:
    var_user_chosen_map_list_ids VARCHAR2(32767);
    PROCEDURE PROCMAPSEARCH (par_user_chosen_map_list_ids PKG_ARR_MAPS.ARR_MAP_LIST)
    IS
    CURSOR cur_map_search (par_user_chosen_map_list_ids IN NUMBER)
    IS
    SELECT MI.map_date
           MT.map_title,
    FROM map_info MI,
         map_title MT,
    WHERE MI.map_title_id = MT.map_title_id
    AND MI.map_publication_id IN
    (var_user_chosen_map_list_ids);
    var_map_list_to_compare VARCHAR2(32767) := '';
    var_exe_imm_map VARCHAR2(32767);
    BEGIN
    FOR rec_user_chosen_map_list_ids IN 1 .. par_user_chosen_map_list_ids.count
    LOOP
       var_user_chosen_map_list_ids := var_user_chosen_map_list_ids ||
       '''' ||
       par_user_chosen_map_list_ids(rec_user_chosen_map_list_ids) ||
    END LOOP;
    var_user_chosen_map_list_ids := substr(var_user_chosen_map_list_ids,
                                            1,
                                            length(var_user_chosen_map_list_ids)-1);
    var_exe_imm_map := 'FOR rec_search_entered_details IN cur_map_search
    LOOP
    htp.print('Map date: ' || cur_map_search.map_date || ' Map title: ' || cur_map_search.map_title)
    END LOOP;';
    END PROCMAPSEARCH;EXECUTE IMMEDIATE var_exe_imm_map;
    I would be grateful of any comments or advice.
    Kind regards
    Tim

    I would like to thank everyone for their kind help.
    I have now successfully converted my code for use with dynamic SQL. Part of my problem was getting the concept confused a little, especially as I could get everything work in a static cursor, including variables, as long as they did not contain multiple values. I have learnt that dynamic sql runs the complete select statement at runtime. However even with this I was getting concepts confused. For example I was including variables and the terminator; inside my select string, where as these should be outside it. For example the following is wrong:
         TABLE (sys.dbms_debug_vc2coll(par_user_chosen_map_list_ids))....
    AND MI.map_publication_id = column_value;';Where as the following is correct:
         TABLE (sys.dbms_debug_vc2coll('||par_user_chosen_map_list_ids||'))....
    AND MI.map_publication_id = column_value';PL/SQL is inserting the values and then running the select statement, as opposed to running the select statement with the variables and then accessing the values stored in those variables. Once I resolved that it worked. My revised code is as follows:
    --Global variable section contains:
    var_user_chosen_map_list_ids VARCHAR2(32767);
    var_details VARCHAR(32767);
    PROCEDURE PROCMAPSEARCH (par_user_chosen_map_list_ids PKG_ARR_MAPS.ARR_MAP_LIST)
    IS
    BEGIN
    FOR rec_user_chosen_map_list_ids IN 1 .. par_user_chosen_map_list_ids.count
    LOOP
       var_user_chosen_map_list_ids := var_user_chosen_map_list_ids ||
       '''' ||
       par_user_chosen_map_list_ids(rec_user_chosen_map_list_ids) ||
    END LOOP;
    var_user_chosen_map_list_ids := substr(var_user_chosen_map_list_ids,
                                            1,
                                            length(var_user_chosen_map_list_ids)-1);
    var_details := FUNCMAPDATAFIND (var_user_chosen_map_list_ids);
    htp.print(var_details);
    END PROCMAPSEARCH;
    FUNCTION FUNCMAPDETAILS (par_user_chosen_map_list_ids IN VARCHAR2(32767)
    RETURN VARCHAR2
    AS
    TYPE cur_type_map_search IS REF CURSOR;
    cur_map_search cur_type_map_search;
    var_map_date NUMBER(4);
    var_map_title VARCHAR2(32767);
    begin:
    OPEN cur_map_search FOR
    'SELECT MI.map_date,
           MT.map_title
    FROM map_info MI,
         map_title MT,
         TABLE (sys.dbms_debug_vc2coll(' || par_user_chosen_map_list_ids || '))
    WHERE MI.map_title_id = MT.map_title_id
    AND MI.map_publication_id = column_value';
    LOOP
    FETCH cur_map_compare INTO
    var_map_date,
    var_map_title;
    var_details := var_details || 'Map date: '||
                        var_map_date ||
                        'Map title: ' ||
                        var_map_title;
    EXIT WHEN cur_map_compare%NOTFOUND;
    END LOOP;
    RETURN var_details;
    END FUNCMAPDETAILS;Kind regards
    Tim

  • Where clause question

    Hi Experts,
    We have a typical requirement for including the where clause dynamically. Here is the Scenario:
    We will be getting the Where Clause as a string parameter value from reporting framework. This needs to be appended in Crystal Reports with Select Clause.
    Here is an example. We will be getting the Where parameter value as
    clnt_id=1001 and acct_no=232 and cust='XYZ'
    This will be passed as a single string to CR which needs to act as Where Clause while the CR query hits database.
    Not sure how to implement this. Any suggestions will be of great help.
    Thanks
    Naresh

    As far as I'm aware, there are no other solutions, unless you want to parse the Where clause string in Crystal and code the possibilities.  This might be reasonable if there are only a couple of fields that possibly would be used in the where clause of any given report, but tedious and slow if there are hundreds.  Report performance will suffer in any case because the database will not be performing the Where clause, so more data will be passed back to Crystal which it will have to determine needs to be dropped.
    I don't know how many existing reports you're dealing with, but it might not be overly time consuming to change the reports to be based on an SQL Command.  After all, Crystal can give you the SQL that it intends to use when a report is defined based on tables and views...  IMHO, I see the benefit of using an SQL Command and have the database do the Where clause worth while in this case...
    HTH,
    Carl

  • Problem in adding one condition in where clause

    Hi,
    I am populating this internal table t_bkpf for all entries in gt_covp_ext
      select bukrs belnr gjahr
             bldat budat cpudt
             xblnr waers awtyp awkey
             from bkpf
             into corresponding fields of table t_bkpf
             for all entries in gt_covp_ext
             where bukrs eq gt_covp_ext-bukrs and
                   awtyp eq 'MKPF'.
    Here i have to add one more condition in where clause
    AWKEY = ( Concatenated string of gt_covp_ext-REFBN + gt_covp_ext-REFGJ )
    As i am not using loop at gt_covp_ext.How to implement this condition in Where clause.
    Please help.If you did not understood the requirement reply this post.
    Mukesh Kumar
    Message was edited by:
            mukesh kumar

    Hi,
    Create a new internal table gt_covp_ext_new with the same structure as gt_covp_ext. Include an extra field in gt_covp_ext_new-concat,  to store the concatenated value.
    Copy all entries from gt_covp_ext to gt_covp_ext_new.
    Loop at gt_covp_ext.
    move-corresponding gt_covp_ext to gt_covp_ext_new.
    gt_covp_ext_new-concat = gt_covp_ext-REFBN + gt_covp_ext-REFGJ .
    append gt_covp_ext_new.
    endloop.
    Now use this new internal table in the query.
    select bukrs belnr gjahr
    bldat budat cpudt
    xblnr waers awtyp awkey
    from bkpf
    into corresponding fields of table t_bkpf
    for all entries in gt_covp_ext_new
    where bukrs eq gt_covp_ext_new-bukrs and
    awkey eq  gt_covp_ext_new-concat and
    awtyp eq 'MKPF'.
    Hope this answers your qn.
    Regards,
    Divya

  • SQL Strings in Where Clause - single quotes issue

    All,
    I’m having issues related to SQL String literals when trying to deploy to flash. The complication works fine but I then get a message (see below) stating that a ; is missing. The SQL query and the iview runs fine when we use numeric values or do not apply a where clause on the table.
    I am using Visual Composer that has been packaged with NW2004sSP7_Preview
    VC & Flex Version: 645.7.0.3
    The Error message is get is;
    Error in executing a process for Flex compilation, Error 1033: ';' expected
          (C:usrsapJ2EJC01j2eeclusterserver0GUIMachine_Business_Packagestest_48731FLEX_COMPILATION_FOLEDRAADCN.mxml:269)
    Error 1205: The statement 'Test' is incomplete.
          (C:usrsapJ2EJC01j2eeclusterserver0GUIMachine_Business_Packagestest_48731FLEX_COMPILATION_FOLEDRAADCN.mxml:269)
    Failed to compile AADCN.mxml
    When I goto the deployment file – it has the below line;
    <i>'<Request type="EXECUTE_RELATIONAL" system="BI_JDBC" system_type="SAP_BI_JDBC" maxrows="500" templateid="BIR_SQL"><Objects type="INPUT" shape="OBJ" role="INPUT"><Object type="INPUT_FIELD" id="SQL_STATEMENT" appName="SQL" mapped="0" value=""/></Objects><Objects type="OUTPUT" shape="SET" role="OUTPUT"><Object type="OUTPUT_FIELD" id="name" appName="name"/></Objects><Objects id="1" type="TEMPLATE_PARAMETER"><Object id="2" type="SQL" value="select name from pub.srcompany where name ='Test Company'"/></Objects></Request>';</i>
    It seems that the parser for the flash deployment is prematurely finishing the parse when it hits the single quotation in the SQL string!
    We had a similar error where if we had a carriage return in the SQL (e.g. between the from & the where clause) then the deployment parser was stating that the line finished prematurely. In the deployment file the carriage return forced a new line thus making the message incomplete. Removing the carriage return resolve that issue
    The SQL Preview in the SQL Editor within VC works fine with the string literals in the where clause.
    The functionality compiles & deploys in web dynpro however it does not return the results to the table
    Questions
    1:> Has anyone successfully used flash with string literals in the SQL where clause or had seen this issue in the past?
    2:> Is there a setting to get the SQL working on Web dynpro for the information to be returned that I may have missed?
    Any assistance would be greatly appreciated.
    Best Regards,
    Ian.

    Hey,
    I have worked with SQL Editor a lot. Here's a how to guide I put together on it:
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/6339e7d4-0a01-0010-1c98-db00e52e989a
    Let me know if that helps...
    Prakash

  • How to validate string parameter in subprogram where clause.

    Hi All,
    I m using forms6i, I have receive one string parameter from some other Form. I want to check this parameter value in my subprograms where clause (Particularly IN clause).
    For Example the parameter name : P_PARA1 and its value 'AA','BB','CC'
    my code like this..
    select ....
    from <tab name>
    wehre ....
    and column_name in(:parameter.para1)*
    plz help me hw can i handle this situation.

    I Did't get any error value, but the in where clause :parameter value added '' quotes at runtime.
    For example
    declare
    cursor c1 is (p_p1 IN VARCHAR2) IS
    select ...
    from ...
    where <columnname> IN (p_p1);
    begin
    open C1(:PARAMETER.P_para1);
    now i have checked the selection query while running form in SQL Monitor, below like that
    select ..
    from ...
    where <columnname> IN ('''AA'','''BB'');
    hw its added the extra ' quote..

  • PASS STRING IN TO WHERE CLAUSE

    Hi All,
    i'm facing a problem when i used
    SELECT * INTO TABLE ITAB FROM T000 WHERE T000~MANDT IN (810, 812, 800).
    then it's gives three values which is right according to requirement.
    but i hve to use a string n pass these values by string so i developed a string V_STRING N IT'S VALUE IS SAME AS 810, 812, 800.
    MEANS
    V_STRING = 810, 812, 800
    BUT WHEN I PASSED THIS STRING TO THIS SELECT THEN IT'S GIVE ONLY ONE VALUE FOR ONLY FIRST VALUE MEANS FOR ONLY 810.
    PLS SUGGEST ME WHY IT'S ONLY THROW ING RESULT ONLY FOR ONE VALUE.
    REGARDS,

    The only dynamic select is allowed by using an internal table of not more than 72 characters, so build the full string WHERE clause like:
    DATA: where_clause TYPE TABLE OF char72,
          clause LIKE LINE OF where_clause.
    PARAMETER p_mandt TYPE char50.
    CONCATENATE 'MANDT IN (' p_mandt ')' INTO clause.
    APPEND clause TO where_clause.
    and then
    SELECT * FROM t000  CLIENT SPECIFIED
      WHERE (where_clause).
    Remenber the limit of 72 char. So if only one field is of concern, you should better build a RANGE type internal table and use it in a IN logical expression of the WHERE clause. (Use SPLIT AT ',')
    DATA it_mandt TYPE TABLE OF char4 WITH HEADER LINE.
    DATA range_mandt TYPE RANGE OF t000-mandt WITH HEADER LINE.
    SPLIT p_mandt AT ',' INTO TABLE it_mandt.
    CLEAR range_mandt.
    range_mandt-sign = 'I'.
    range_mandt-option = 'EQ'.
    LOOP AT it_mandt.
      CHECK NOT it_mandt IS INITIAL.
      CONDENSE it_mandt.
      range_mandt-low = it_mandt.
      APPEND range_mandt.
    ENDLOOP.
    and then
    SELECT * FROM t000  CLIENT SPECIFIED
      WHERE mandt IN range_mandt.
    Regards

  • WHERE clause affects sort order???

    Hi,
    I'm playing around with linguistic sorting and comparing and was using the examples from
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14225/ch5lingsort.htm#g1018324
    So I have the three entries Große, große and Grosse in my table (the sample data from the manual)
    To test these features, I did
    alter session set nls_comp = Linguistic;
    alter session set nls_sort = XGerman_CI; and ran the following select statement:
    SELECT name
    FROM words
    ORDER BY 1;The rows are then returned in the following order:
    Große
    große
    Grosse
    which is exactly what I expect.
    When I run
    SELECT name
    FROM words
    WHERE name = 'Grosse'; it returns all three rows which is expected as well.
    When I add the ORDER BY (which is the same ORDER BY as in the first step):
    SELECT name
    FROM words
    WHERE name = 'Grosse'
    ORDER BY 1; the rows are returned in the following order
    Große
    Grosse
    große
    which is different to the order without the WHERE clause (and wrong)
    The question is: why does the WHERE clause affect the ordering?

    I'm not sure why you think the third sort order is incorrect. In English, the capitals will sort before the lower case leaving both "G" values before the "g". I don't know where the special ... German? ... character will appear but assume that ORDER BY is sorting correctly. If not you need to contact Oracle and open a support ticket.
    WHERE clauses will affect how data comes back by influencing index use, but some of what they do will be random. The only way to be sure data will be returned in a certain order is to use an ORDER BY clause.
    In particular, in 10g the parallel query option is notorious for returnding data in random order. Where in 9i an indexed lookup would tend to return data in an imaginary sorted order the same query using PQO in 10g often returns data in random order.

  • Need help in clearing string buffer during dynamic VO where clause in oaf

    Hi All,
    I am dynamically setting where clause in OAF using string buffer, but the issue is when i am executing the vo multiple times so the old data in the string buffer it is not clearing .so every time my where clause adding to the query and it is erroring out, please help me how to clear string buffer class in oaf.
    Thnaks

    Hi,
    Could you please share the code segment for reference. Then we can tell the solution.
    Regards,
    Tarun

  • How to use string operation in where clause of select query

    Hello All,
    I just want to know how can i write a restriction in select query saying retrive data only begins with name "DE*".
    Explaination: If my table has records and names starts with character then i want to write a query to fetch all the records in which names starts with DE*.
    Thanks in advance for your quick reply...
    Dev.

    Hi
    In the where clause you need to write like
    WHERE NAME LIKE 'DE%'
    Regards
    Sudheer

  • Creating dynamic where clause with string delimiter " ; "

    hi...
    i need a solution for complex problem like, 1. start_date IN date,
    end_date IN date,
    shift_type IN varchar2
    i will get shift_type as "first_shift" or "first_shift;second_shift" or "first_shift;second_shift;third_shift" ....etc any combination. where fist & second & third shits are nothing but 1 , 2 , 3. i need to find out data between start_date and end_date in combination of shifts ( may be 1;2 or 1;3 or 1;2;3 or 2;3 ...etc) . now i need to write this code in dynamic where clause ...i tried in different ways...but not succeeded. can anybody guide me step by step...or with script.
    NOTE: there is a table called "shift" with data like
    shift_type shift_mode
    1 first_shift
    2 second_shift
    3 third_shift

    Hi,
    Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
    If the question involves parameters, give a few different sets of parameters and the results you want for each set, given the same sample data.
    It's unclear that you need dynamic SQL at all.
    If shift_type is a variable, that can be either a single value or a ;-delimited list (such as '1;3'), you can compare that to a column called shift_column like this:
    WHERE        ';' || shift_type   || ';'     LIKE
           '%;' || shift_column || ';%'No dynamic SQL or PL/SQL required.
    If you really do want to use dynamic SQL, these two pages should gives you some ideas:
    http://www.oracle-base.com/articles/misc/DynamicInLists.php
    http://tkyte.blogspot.com/2006/06/varying-in-lists.html

Maybe you are looking for