Sorting technique used by oracle for "order by" clause.

Hi All,
it could be very help to me if you provide some information about sorting technique used by oracle engine for order by clause.
Issue i am facing :
Table : xx
Line Date
1 05-06-2013 00:00:00
2 05-06-2013 00:00:00
when we query above table using order by date, it is returning line 2 prior to line 1. we would like to know why it is returning line 2 first?
Regards,
Ram

>
it could be very help to me if you provide some information about sorting technique used by oracle engine for order by clause.
>
Well ok - but be warned that many people wind up being sorry they ask that question. Hopefully Hemant's answer is what you really wanted.
See 'Linguistic Sorting and String Searching' in the Oracle® Database Globalization Support Guide
http://docs.oracle.com/cd/B28359_01/server.111/b28298/ch5lingsort.htm
Sorting will be controlled by the settings of NLS_LANGUAGE, NLS_SORT and NLS_COMP.
Here is the doc page for NLS_SORT
http://docs.oracle.com/cd/B28359_01/server.111/b28298/ch3globenv.htm#i1008393
>
NLS_SORT specifies the type of sort for character data. It overrides the default value that is derived from NLS_LANGUAGE.
NLS_SORT contains either of the following values:
NLS_SORT = BINARY | sort_name
BINARY specifies a binary sort. sort_name specifies a linguistic sort sequence.
>
And the one for NLS_COMP
http://docs.oracle.com/cd/B28359_01/server.111/b28298/ch3globenv.htm#i1008458
>
The value of NLS_COMP affects the comparison behavior of SQL operations.
You can use NLS_COMP to avoid the cumbersome process of using the NLSSORT function in SQL statements when you want to perform a linguistic comparison instead of a binary comparison. When NLS_COMP is set to LINGUISTIC, SQL operations perform a linguistic comparison based on the value of NLS_SORT. A setting of ANSI is for backward compatibility; in general, you should set NLS_COMP to LINGUISTIC when you want to perform a linguistic comparison.

Similar Messages

  • Using bind varaible for order by clause

    Hello
    Can some one suggest for the following scenario?
    My order by clause will be constructed based on the selected fields in the form. And now the order by clause need to be passed as parameter to Excel report.
    When I use Ref cursor and OPEN-FOR-USING clause, data is not sorted.
    do we have any other alternate for this?
    Please observe the example code for the same. Block below resembles the code for to select the data for excel report.
    SQL> select * from t;
    T
    sdf
    der
    gdr
    ghft
    ytut
    lkrt
    rtrt
    tyrt
    SQL> declare
    2 l_order_by VARCHAR2 (100);
    3 l_test varchar2(10);
    4 TYPE TEST IS REF CURSOR;
    5 c_TEST test;
    6 L_string VARCHAR2(2000):= 'select * from t order by :pi_order';
    7 begin
    8 l_order_by := ' t DEsc';
    9 open c_test for l_string using l_order_By;
    10 loop
    11 fetch c_test into l_test;
    12 exit when c_TEST%notfound;
    13 dbms_output.put_line (l_test);
    14 end loop;
    15 close c_test;
    16 end;
    17 /
    sdf
    der
    gdr
    ghft
    ytut
    lkrt
    rtrt
    tyrt
    PL/SQL procedure successfully completed.
    Cheers
    Ram Kanala

    My order by clause will be constructed based on the selected fields in the formDoes this look like you need ?
    SQL> var so number
    SQL> exec :so := 1;
    PL/SQL procedure successfully completed.
    SQL> set serveroutput on
    SQL> declare
      2   rc sys_refcursor;
      3   type emprec is table of emp%rowtype index by pls_integer;
      4   erec emprec;
      5  begin
      6 
      7   open rc for 'select * from emp order by ' ||
      8   'decode(:p,1,ename,2,deptno,3,sal,null),' ||
      9   'decode(:p,4,ename,5,deptno,6,sal,null) desc' using :so,:so;
    10   fetch rc bulk collect into erec;
    11   close rc;
    12 
    13   for i in 1..erec.count loop
    14    dbms_output.put_line('Ename = ' || erec(i).ename || ', deptno = ' || erec(i).deptno || ', sal
    = ' || erec(i).sal);
    15   end loop;
    16  end;
    17  /
    Ename = ADAMS, deptno = 20, sal = 1100
    Ename = ALLEN, deptno = 30, sal = 1600
    Ename = BLAKE, deptno = 30, sal = 2850
    Ename = CLARK, deptno = 10, sal = 2450
    Ename = FORD, deptno = 20, sal = 3000
    Ename = JAMES, deptno = 30, sal = 950
    Ename = JONES, deptno = 20, sal = 2975
    Ename = KING, deptno = 10, sal = 5000
    Ename = MARTIN, deptno = 30, sal = 1250
    Ename = MILLER, deptno = 10, sal = 1300
    Ename = SCOTT, deptno = 20, sal = 3000
    Ename = SMITH, deptno = 20, sal = 800
    Ename = TURNER, deptno = 30, sal = 1500
    Ename = WARD, deptno = 30, sal = 1250
    PL/SQL procedure successfully completed.
    SQL> exec :so := 2
    PL/SQL procedure successfully completed.
    SQL> /
    Ename = CLARK, deptno = 10, sal = 2450
    Ename = KING, deptno = 10, sal = 5000
    Ename = MILLER, deptno = 10, sal = 1300
    Ename = JONES, deptno = 20, sal = 2975
    Ename = FORD, deptno = 20, sal = 3000
    Ename = ADAMS, deptno = 20, sal = 1100
    Ename = SMITH, deptno = 20, sal = 800
    Ename = SCOTT, deptno = 20, sal = 3000
    Ename = WARD, deptno = 30, sal = 1250
    Ename = TURNER, deptno = 30, sal = 1500
    Ename = ALLEN, deptno = 30, sal = 1600
    Ename = JAMES, deptno = 30, sal = 950
    Ename = BLAKE, deptno = 30, sal = 2850
    Ename = MARTIN, deptno = 30, sal = 1250
    PL/SQL procedure successfully completed.
    SQL> exec :so := 5;
    PL/SQL procedure successfully completed.
    SQL> /
    Ename = BLAKE, deptno = 30, sal = 2850
    Ename = TURNER, deptno = 30, sal = 1500
    Ename = ALLEN, deptno = 30, sal = 1600
    Ename = MARTIN, deptno = 30, sal = 1250
    Ename = WARD, deptno = 30, sal = 1250
    Ename = JAMES, deptno = 30, sal = 950
    Ename = SCOTT, deptno = 20, sal = 3000
    Ename = JONES, deptno = 20, sal = 2975
    Ename = SMITH, deptno = 20, sal = 800
    Ename = ADAMS, deptno = 20, sal = 1100
    Ename = FORD, deptno = 20, sal = 3000
    Ename = KING, deptno = 10, sal = 5000
    Ename = MILLER, deptno = 10, sal = 1300
    Ename = CLARK, deptno = 10, sal = 2450
    PL/SQL procedure successfully completed.Rgds.

  • Can I use an expression in order by clause

    SELECT
    PERIOD_NUM, period_num *12,
    period_name,
    --TO_DATE(SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-'),3),'MON-RR'),
    TO_NUMBER(TO_CHAR(TO_DATE((SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-')+1,2)),'MONRR'),'RRRRMM'))
    FROM GL.GL_PERIODS
    order by 2--TO_NUMBER(TO_CHAR(TO_DATE(SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-')+1,2),'MONRR'),'RRRRMM'))
    desc
    gl_periods is a table, period_name consists of values like oct-01_fy-01 etc and oct01_01_fy-01
    I want to sort on this column based on period_name, so I am trying to convert into number like for oct-01_fy-01, the query gives
    200110 and so on. the recent period comes first, But my question is I can't use the same to_number(.....) in the order by clause. oracle gives an error
    ORA-01843: not a valid month.
    Let me know whether anyone come across with this kind of situation

    If you are sure the expression is correct, since you are using it in the select clause, give an alias and use the alias in the order by clause.
    null

  • Using ROWID in the ORDER BY clause

    Hi,
    I can consider myself as a PL/SQL beginner. I need to query some existing tables created for audit scope.
    These tables have more duplicate rows respect to the same audit date: the precision is at "hour and minute and second" level.
    But I need to understand which is the more recent row for the same audit date. So I think to use in the SELECT statement
    the clause ORDER BY in this manner:
    SELECT ... FROM Audit_Table ORDER BY Audit_Date desc, ROWID desc.
    I'd like to use this ORDER BY also for the ROW_NUMBER function.
    Any suggests to me about this topic? Many thanks

    I don't need to provide an example, I can do better by pointing you to the documentation:
    http://docs.oracle.com/database/121/SQLRF/pseudocolumns008.htm#SQLRF00254
    "If you delete a row, then Oracle may reassign its rowid to a new row inserted later" - for example
    I can point you to asktom: https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:912210644860
    "ROWIDs are NOT sortable" - for example
    Your example just happened to work, but you are using the rowid semantically incorrect.

  • Database Settings for order By Clause

    Hi,
    In oracle if we do a "order by" in SQL statement for field values whose values are empty, then they would be listed at the last if we dont specify any NVL() for it.
    Is there any other option by which the funcionality of Order by clause with Empty field values can be changed.
    Is there any settings that can be made in the existing database , so that the Order by clause does not list the empty field values at the bottom.
    Thanx in advance.
    Bharath

    you can do wonders with decode and nvl
    for instance:
    order by decode(nullable_field, null, 1, 2)would make all your null fields come first, whereas
    order by nvl(nullable_field, 'M')would make them appear in the middle of an alphabetical list.
    Basically NULLs are not something. You have to give the ORDER BY something to replace the null.
    null

  • Passing parameter in report for order by clause

    how can we pass the name of the column on which we want the order by clause to sort as a parameter through parameter from in a report??

    Hi Guptha,
    We can create a bind parameter in report and we can send it as a parameter or enter in the parameter form.
    The Query will be
    select * from emp
    order by :Order_Bu_Column
    I think it will help you.
    Regards,
    Siva.

  • How to use XML Gateway for Order Import and Item Import?

    Hello XML gurus,
    I'm working in Oracle Apps area and do not have much knowledge on XML or web programming. Currently in my project there is a need to use Order Import and Item Import using interface tables and XML Gateway. The items/orders information will be in the XML format from Siebel system, there will be no custom processing...just process the data as-is.
    I've read the XML Gateway user guide but did not understand much.
    Anyone with similar implementation experience?
    I'm looking for a step-by-step guide on what needs to be done in Oracle Apps side to populate the interface tables from XML file and call the import APIs?
    Your inputs, suggestions, tips are highly appreciated. :)
    Thx,
    Jags

    I have a similar requirement to automate the order import and was looking at XML Gateway. Please update if you were able to find more information.
    - Ayyappa

  • Help for order by clause

    Hi ,
    i have a below table with data , when i run my select with order by option its gives me
    1     Card
    as a first row but i need this
    12     atm
    as a first row ,how this is possible .
    create table test
    (ID number ,
    val varchar2(20)
    insert into test values (1,'Card')
    insert into test values (2,'Card')
    insert into test values (3,'Card')
    insert into test values (4,'Cash')
    insert into test values (5,'Cash')
    insert into test values (6,'Cash')
    insert into test values (7,'Checque')
    insert into test values (8,'Checque')
    insert into test values (9,'Checque')
    insert into test values (10,'atm')
    insert into test values (11,'atm')
    insert into test values (12,'atm')
    select * from test
    order by val asc

    YOu can also use Nlssort like
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    SQL> select *
      2  from test
      3  order by nlssort(val, 'NLS_SORT = WEST_EUROPEAN_AI') asc, id desc
      4  /
            ID VAL
            12 atm
            11 atm
            10 atm
             3 Card
             2 Card
             1 Card
             6 Cash
             5 Cash
             4 Cash
             9 Checque
             8 Checque
            ID VAL
             7 Checque
    12 rows selected.

  • Order By clause in Oracle

    Hello,
    Sorry I am posting it for the second time
    In Oracle , the order by clause does not return the expected query result, if any of the field value in the order by clause has an empty string.
    Oracle treats the empty string as null value and ORDER BY gives a result with the empty string field values listed at last.
    For example :
    test is a sample table containing "name" field.
    Query: "select name from test order by name"
    In SQL Server/Access
    Result (1)
    NAME
    (blank)
    (blank)
    User1
    User2
    User3
    In Oracle
    Result (2)
    NAME
    User1
    User2
    User3
    (blank)
    (blank)
    I know some of the Work arounds for this as listed below :
    1) To use NVL in Order By Clause.
    i.e., the modified query
    "select name from test order by nvl(name,0)"
    gives the result same as Result (1).
    2) To have single blank space in the field value if it is empty.
    I dont want to use either of these two options b'se it would lead to a mass change in my existing code.
    Is there any way i can do a
    collation order settings in the Oracle databases to get the results as in MS SQL/Access.
    Or Is there any other way that i can set the
    option once for this and need not worry abt it afterwards?
    Can Any help me out in Solving this?
    Thanks
    Devi
    null

    For fun and out of curiosity, I tried :
    SQL> select /*+ leading(t) use_nl(e t) */
      2         e.deptno
      3       , e.empno
      4  from scott.emp e
      5     , table(sys.odcinumberlist(30,10,20)) t
      6  where e.deptno = t.column_value
      7  ;
    DEPTNO EMPNO
        30  7499
        30  7521
        30  7654
        30  7698
        30  7844
        30  7900
        10  7782
        10  7839
        10  7934
        20  7369
        20  7566
        20  7902
    12 rows selected
    It "seems" to work but of course we can't expect a consistent behaviour, or can we? ;)

  • Order By Clause with Empty Field values !

    Hello,
    In Oracle , the order by clause does not return the expected query result, if any of the field value in the order by clause has an empty string.
    Oracle treats the empty string as null value and ORDER BY gives a result with the empty string field values listed at last.
    For example :
    test is a sample table containing "name" field.
    Query: "select name from test order by name"
    In SQL Server/Access
    Result (1)
    NAME
    (blank)
    (blank)
    User1
    User2
    User3
    In Oracle
    Result (2)
    NAME
    User1
    User2
    User3
    (blank)
    (blank)
    I know some of the Work arounds for this as listed below :
    1) To use NVL in Order By Clause.
    i.e., the modified query
    "select name from test order by nvl(name,0)"
    gives the result same as Result (1).
    2) To have single blank space in the field value if it is empty.
    I dont want to use either of these two options b'se it would lead to a mass change in my existing code.
    Is there any way i can do a
    collation order settings in the Oracle databases to get the results as in MS SQL/Access.
    Can Any help me out in Solving this?
    Thanks
    Devi Shankar
    null

    Bharath,
    I am moving this question to the SQL forum.
    Regards,
    Geoff

  • Materialized view and Order by clause

    Hi all. I'd like to have some information regarding the order by clause used in a materialized view.
    I'm using Oracle 9.2 and Win2003 server. I have a common view (my_common_view) on which a materialized view is based (my_materialized_view). Materialized view is built for fast refresh and is created in this way:
    create materialized view my_materialized_view as
    select * from my_common_view
    order by 1,2,3;
    My question is: if I query this materialized view without adding an order by clause in the statement, I will obtain always an ordered result set? So, is it useful to include the order by clause in the materialized view script or the order by has to be include in the statement used for quering the materialized view (for example like this: select * from my_materialized view order by ...)?
    Thank you very much.
    Ste.

    SQL> create table t
      2  (x int)
      3  /
    Table created.
    SQL> create view vw
      2  as
      3  select *
      4    from t
      5   order by x
      6  /
    View created.
    SQL> select *
      2    from v$version
      3  /
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
    PL/SQL Release 9.2.0.7.0 - Production
    CORE    9.2.0.7.0       Production
    TNS for IBM/AIX RISC System/6000: Version 9.2.0.7.0 - Production
    NLSRTL Version 9.2.0.7.0 - Production
    SQL>

  • Sys_context and Order by clause

    Hi All,
    Im using Oracle 11g R2.
    Is it somehow possible to use Context variable (to store column name) in ORDER BY clause? My ultimate aim is to construct Order by at runtime without using concatenation.
    e.g. i created context as my_ctx for some pkg and its variable as v_ctx having value set as "TABLE_NAME"
    so is it possible to use it as
    select * from all_tables
    order by sys_context('my_ctx', 'v_ctx');
    if no, is there any work around??
    Thanks,
    Vivek

    Hi, Vivek,
    Vivek wrote:
    Hi All,
    Im using Oracle 11g R2.
    Is it somehow possible to use Context variable (to store column name) in ORDER BY clause? My ultimate aim is to construct Order by at runtime without using concatenation.Sure, you can use SYS_CONTEXT in an ORDER BY clause, but it's unclear what you're trying to do, so I don't know if SYS_CONTEXT will help you.
    e.g. i created context as my_ctx for some pkg and its variable as v_ctx having value set as "TABLE_NAME"
    so is it possible to use it as
    select * from all_tables
    order by sys_context('my_ctx', 'v_ctx');
    if no, is there any work around??Post a complete test script hat people can run to re-create the problem and test their ideas. Show the output you want from the parameters an data you give.
    Think about using a CASE expression in the ORDER BY clause. You can use SYS_CONTEXT in the CASE expression if you want to.

  • Default where with order by clause

    Hi all
    How to use Default where with order by clause
    for example i want to use Default_where clause with order by clause
    Regards
    Shahzaib ismail

    Hi,
    I have a similar query for where clause.
    my query-find form is a multi record form like below:
    Criteria - Condition - Value
    Item/Category/Planner (List_Item) - Equals/Among (List_Item) - text_FIELD
    Item - Equals - ITEM_1
    Category - Among - ('CAT1', 'CAT2')
    Planner - Equals - PL1
    Find_BUTTON Clear_BUTTON
    User can select any criteria and condition combination and then enter the value in "Value" text field.
    My query is: how can I prepare a where clause based on user input since the user can enter any number of values/combinations? I guess I would have to use some looping to generate where clause.
    Thanks
    Imran

  • Group by and order by clause

    Hi,
    I have wriiten the below query and i am using group by and order by clause....but i am not getting the required result
    SELECT b.ACCOUNT_REGION,CASE WHEN b.product_reference_status='Reference' THEN 'Recruited' WHEN b.product_reference_status in ('Inactive','Declined') THEN 'Inactive' WHEN b.product_reference_status ='Nominate' THEN 'Inprogress' ELSE 'Other' END product_reference_status, COUNT(decode(b.product_quarter,'Q1FY09',b.REFERENCE_ID)) Q1FY09, COUNT(decode(b.product_quarter,'Q2FY09',b.REFERENCE_ID)) Q2FY09, COUNT(decode(b.product_quarter,'Q3FY09',b.REFERENCE_ID)) Q3FY09, COUNT(decode(b.product_quarter,'Q4FY09',b.REFERENCE_ID)) Q4FY09, COUNT(decode(b.product_quarter,'Q1FY10',b.REFERENCE_ID)) Q1FY10, COUNT(decode(b.product_quarter,'Q2FY10',b.REFERENCE_ID)) Q2FY10, COUNT(decode(b.product_quarter,'Q3FY10',b.REFERENCE_ID)) Q3FY10, COUNT(decode(b.product_quarter,'Q4FY10',b.REFERENCE_ID)) Q4FY10, COUNT(b.product_quarter) Total
    FROM refdump a, ref_dh_pr b
    WHERE A.REFERENCE_ID=b.REFERENCE_ID AND (b.CREATED_ON - NVL(a.REFERENCE_DATE,a.CREATED))>15 group by b.account_region,product_reference_status order by 2
    output is as given below
    Region Status Count
    LAD     Inactive     0
    EMEA     Inactive     21
    *APAC     Inactive     2
    *APAC     Inactive     1
    EMEA     Inactive     2
    EMEA     Inprogress     220
    LAD     Inprogress     19
    LAD     Other     2
    LAD     Other     0
    LAD     Other     5
    LAD     Recruited     182
    APAC     Recruited     191
    My question is
    1) Why i am geeting two APAC regions and 3 LAD (marked *) regions when i have grouped by region and status
    2) How can i make the status in the following order
    a) Recruited
    b)Inprogress
    c)Inactive
    d)Other
    Thanks,

    <i>group by b.account_region,product_reference_status</i>
    Just take all the columns in the group by clause in your select segment and run the SQL, you'll come to know abt the different values in columns which group together.
    *009*

  • Default sorting order used by oracle

    Can anyone please let me know whether oracle uses any default sort order or not when executing an sql which does not have any sort order/ order by clause?
    I have a sql query where no sort order is specified.
    Now if I add some more columns in the select clause and execute the same query, the number of records returned are same and also the records are same.
    But the order in which they are displayed is different from the previous query.
    Could anyone please let me know whether oracle uses any default sort order or not when executing an sql which does not have any sort order/ order by clause
    or does it depend on the columns of the table?
    Thanks in advance.

    To answer your direct question "why the order in the previous example differ".
    When you added additional columns to the select list, that changed the query plan generated for the query. This caused Oracle to pick up the rows in a different order. There are a number of reasons this could happen, the new selected columns caused a different index (or no index) to be used for one of the tables in the query, it caused a different table to be be used as the "driving" table (i.e. the first one accessed), it caused a different join method to be used.
    HTH
    John

Maybe you are looking for

  • HELP!!! I Can't Authorize My Nook

    I've tried everything posted on this site to get my nook authorized to read the books from the public library. I deleted Adobe and relaunched it,but it still won't authorize my device. Any advice would be helpful. I'm ready to return it, but it's for

  • How to create a video thumbnail???

    I'm pretty new to FCP but I've just finished several shorts and I'm getting ready to upload them to my web site and other video sites. I would like a specific frame to appear as the video thumbnail. (In other words: the "preview frame" that appears i

  • Flash player issue

    how do i down load adobe flash player an make it work

  • Laptop wont recognise Ipod

    my Ipod 5G won't be recognised my laptop. the laptop makes the noise signalling when i plug it in but it wont show up in devices??

  • Drill Down Report issues in VS 2010, CR viewer 12

    We are using VS 2010, Crystal Reports 12 with Crystal Viewer 12 control called from a C# programmed page frame.  Several Drill Down reports are developed with CR 12 service pack 1 and then deployed onto the server.  These Drill Down reports work grea