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


Sort Order - Include Null al last


Sort Order - Include Null al last

Author
Message
sl.sajeev
sl.sajeev
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 15
Comments posted to this topic are about the item Sort Order - Include Null al last
Shahnaaz Nazerali-Larsen
Shahnaaz Nazerali-Larsen
Right there with Babe
Right there with Babe (734 reputation)Right there with Babe (734 reputation)Right there with Babe (734 reputation)Right there with Babe (734 reputation)Right there with Babe (734 reputation)Right there with Babe (734 reputation)Right there with Babe (734 reputation)Right there with Babe (734 reputation)

Group: General Forum Members
Points: 734 Visits: 19
The statement :
Select EmpName, DateOfLeaving from Employees order by DateOfLeaving desc, EmpName asc

will not return the date in ascending order as shown by the result set in the question!!

So there is only one correct answer to this question.
Tom Garth
Tom Garth
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2019 Visits: 1499
Select EmpName, DateOfLeaving from Employees order by DateOfLeaving desc, EmpName asc


That's just wrong. Hehe

DECLARE @Employees table (
EmpName varchar(5),
DateOfLeaving datetime
)

insert into @Employees select 'Abc', '10 Oct 1999'
insert into @Employees select 'Bcd', '11 Nov 1998'
insert into @Employees select 'Ccd', NULL
insert into @Employees select 'Dcd', '10 Aug 2000'
insert into @Employees select 'Eed', NULL

Select
EmpName,
DateOfLeaving
from @Employees
order by DateOfLeaving desc, EmpName asc

Dcd 2000-08-10 00:00:00.000
Abc 1999-10-10 00:00:00.000
Bcd 1998-11-11 00:00:00.000
Ccd NULL
Eed NULL


Only the 2nd chioice
Select
EmpName,
DateOfLeaving
from @Employees
order by isnull(DateOfLeaving, '10/10/9999'),EmpName asc

will give the desired output...
Bcd 1998-11-11 00:00:00.000
Abc 1999-10-10 00:00:00.000
Dcd 2000-08-10 00:00:00.000
Ccd NULL
Eed NULL

Wink

Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers

Jeffrey Williams 3188
Jeffrey Williams 3188
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19745 Visits: 10042
Based upon how the question was stated, and the example given - only the second one is correct. The third answer does not return the result with DateOfLeaving in ascending order.

That is just wrong.

Jeffrey Williams
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

Nick Beagley
Nick Beagley
Say Hey Kid
Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)

Group: General Forum Members
Points: 695 Visits: 266
i was sure i chose the 2nd one and was suprised when i got it wrong. But it said the correct answer was the one i thought i selected (the 2nd). So i pressed back in my browser and it was the 2nd one that i had ticked.

oh well..



Iggy-SQL
Iggy-SQL
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3251 Visits: 440
the third answer is incorrect, because it would have put the latest date of leaving first, instead of the earliest date of leaving first. Thus it will contradict the question it self

sorted ascending order of date of leaving


oh well, if the author doesn't read this post then at least it's only 1 point gone.


Urbis, an urban transformation company
antony-688446
antony-688446
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 371
I also picked option 2, only to be told I was wrong, and should have picked option 2!Crying
Iggy-SQL
Iggy-SQL
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3251 Visits: 440
antony (6/22/2008)
I also picked option 2, only to be told I was wrong, and should have picked option 2!Crying


I think you misread the answer. The answer according to the author is 2 and 3. It's the way it's displayed seems to be just a single answer.


Urbis, an urban transformation company
Brian Pedaci
Brian Pedaci
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 182
Glad I wasn't the only one who thought the answer was wrong.
brewmanz
brewmanz
Right there with Babe
Right there with Babe (771 reputation)Right there with Babe (771 reputation)Right there with Babe (771 reputation)Right there with Babe (771 reputation)Right there with Babe (771 reputation)Right there with Babe (771 reputation)Right there with Babe (771 reputation)Right there with Babe (771 reputation)

Group: General Forum Members
Points: 771 Visits: 406
Can someone *PLEASE* check that the Q posers actually read what they write.
Or maybe it's me that's can't read properly. If so, can someone please explain to me how a descending date sort ('Select EmpName, DateOfLeaving from Employees order by DateOfLeaving desc, EmpName asc') can produce an ascending date order ('and all others sorted ascending order of date of leaving')
Of course, once again, the English is not very clear, and is subject to misinterpretation (as mentioned in a previous topic post), but in making it read in proper English like what I do (cf Ernie Wise for this grammatical construct), I cannot see how the 3rd answer offered can possible be correct.
Maybe the poser is confusing 'date' with 'age'?
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