Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

COLLATE order of NULL Expand / Collapse
Author
Message
Posted Monday, May 17, 2010 12:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 19, 2010 4:50 AM
Points: 8, Visits: 14
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)
Post #922719
Posted Monday, May 17, 2010 12:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 1:28 AM
Points: 2,105, Visits: 5,393
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/
Post #922725
Posted Monday, May 17, 2010 3:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 19, 2010 4:50 AM
Points: 8, Visits: 14
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:
[code = "vb"]
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
[/code]
<t1>_1 is an existing table name, <t1>_2 is the created (sorted) table.

by the way: how is this formatting of code section [code] working, for me it seems it is not working ... see above.
Post #922760
Posted Monday, May 17, 2010 3:28 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 1:28 AM
Points: 2,105, Visits: 5,393
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/
Post #922767
Posted Tuesday, May 18, 2010 2:15 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Saturday, July 26, 2014 6:57 AM
Points: 1,502, Visits: 2,670
Thomas, are you sure that the field is NULL and not a zero-length string ("") - ?
Post #923898
Posted Wednesday, May 19, 2010 4:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 19, 2010 4:50 AM
Points: 8, Visits: 14
yes, but I could change it if it would help
Post #924172
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse