SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


COLLATE order of NULL


COLLATE order of NULL

Author
Message
thomas.schoder
thomas.schoder
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 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)
Adi Cohn
Adi Cohn
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8181 Visits: 6594
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/
thomas.schoder
thomas.schoder
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 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.
Adi Cohn
Adi Cohn
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8181 Visits: 6594
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/
WILLIAM MITCHELL
WILLIAM MITCHELL
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3884 Visits: 3083
Thomas, are you sure that the field is NULL and not a zero-length string ("") - ?
thomas.schoder
thomas.schoder
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 14
yes, but I could change it if it would help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search