| Author |
Topic |
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-22 : 23:52:08
|
| hi all,i failed to find any topic related on what im trying to do :-how do i order by column1 (that is varchar column) with <NULL> at the bottom without DESC ?abc<NULL><NULL>NOT <NULL><NULL>abcthanks~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-22 : 23:55:29
|
[code]DECLARE @TABLE TABLE( col varchar(10))INSERT INTO @TABLESELECT NULL UNION ALLSELECT 'a' UNION ALLSELECT NULL UNION ALLSELECT 'b' UNION ALLSELECT 'c' SELECT *FROM @TABLEORDER BY CASE WHEN col IS NOT NULL THEN 1 ELSE 2 END, col/*col ---------- abcNULLNULL*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-07-22 : 23:59:11
|
| We need to order the column by giving 5the max value for the null..Select column from tablenameorder by Isnull(column, 'zzz')--------------------------------------------------S.Ahamed |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-23 : 00:03:08
|
| oh thanks... how do i do it with distinct :(~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-07-23 : 00:05:14
|
[code]DECLARE @TABLE TABLE( col varchar(10))INSERT INTO @TABLESELECT NULL UNION ALLSELECT 'a' UNION ALLSELECT NULL UNION ALLSELECT 'b' UNION ALLSELECT 'c' SELECT colFROM( SELECT DISTINCT col FROM @TABLE) aORDER BY CASE WHEN col IS NOT NULL THEN 1 ELSE 2 END, col/*col ---------- abcNULL*/SELECT colFROM @TABLEGROUP BY colORDER BY CASE WHEN col IS NOT NULL THEN 1 ELSE 2 END, col/*col ---------- abcNULL*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-23 : 00:16:44
|
| got it .. thanks guys~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-23 : 01:48:02
|
Guys,,., im having another problem : Why both of this solution return me error :-"ORDER BY items must appear in the select list if SELECT DISTINCT is specified."SELECT distinct consigneeidFROM itemdetailsGROUP BY consigneeidORDER BY CASE WHEN consigneeid IS NOT NULL THEN 1 ELSE 2 END, consigneeidSELECT distinct consigneeidFROM itemdetailsORDER BY isnull(consigneeid,'zzz') ~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-07-23 : 02:00:35
|
| See Tan's second post or u have to use derived table as belowSelect c1 From(select distinct c1 from @t) as aorder by Isnull(c1,'zz')--------------------------------------------------S.Ahamed |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-07-23 : 02:11:54
|
| oopss... actually it worked already... just i forgot to remove DIstinct when group is in use... sorry~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
|