COLLATE order of NULL

  • NULL entries should be sorted after the value containing columns and not before, when the value containing column that is sorted, is sorted ascending

    what collation can be used on ORDER BY statement

    or how can I manipulate an existing collation so that NULL is treated as behind Z (asc)

  • I wouldn’t change collation just to modify the NULL location in a recordset. Instead I would use a case statement or isnull function. Bellow are 2 examples:

    SELECT WhatEver

    FROM MyTable

    Where Something = X

    ORDER BY ISNULL(StringColmn,'zzzzz')

    SELECT WhatEver

    FROM MyTable

    WHERE Something = X

    ORDER BY CASE WHEN StringColumn IS NULL THEN 1 ELSE 0 END, StringColumn

    Notice that such select statement won’t be able to use the index, so if you your query is not selective (or if you use select top), you’ll might have performance problems.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • thanks, sounds good and I tried it.

    But both versions are not giving the result. It was no error but simply the same as if I would sort without this ISNULL or CASE statements. Maybe it has to do with the environment where I use it. I send it with MS Access 2003. Thats why I cross posted this in the Access section here. But an admi has closed it. I hope we can find the solution here.

    Here I give the full example:

    sql = "SELECT Bez, Mod, Art, Far, Count(Gro) Total

    "INTO " & t1 & "_2 " & _

    "FROM " & t1 & "_1 " & _

    "GROUP BY Bez, Mod, Art, Far " & _

    "ORDER BY CASE WHEN Bez IS NULL Then 1 ELSE 0 END, Bez, Mod, Art, Far "

    DoCmd.RunSql sql

    <t1>_1 is an existing table name, <t1>_2 is the created (sorted) table.

    by the way: how is this formatting of code section working, for me it seems it is not working ... see above.

  • It could be that you are using a control that does its own sorting or just doesn’t use the order of the rows that it got, but the queries should return the rows with null values being the last. The code bellow has a small demo that shows it (you should run it from SQL Server management studio).

    declare @DemoTbl table (i int identity(1,1), ch char(1))

    insert into @DemoTbl (ch)

    select 'b'

    union all select null

    union all select 'a'

    union all select null

    union all select 'c'

    union all select 'd'

    select *

    from @DemoTbl

    order by case when ch is null then 1 else 0 end, ch

    select *

    from @DemoTbl

    order by ISNULL(ch,'zzzz'), ch

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thomas, are you sure that the field is NULL and not a zero-length string ("") - ?

  • yes, but I could change it if it would help

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply