If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Order By (Conside 0 as biggest)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-21-12, 19:01
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
Order By (Conside 0 as biggest)

hi
i want to 'order by asc' a result set by a numeric field, some of this numbers are 0 & i want these zeros come at the end of result set, something like this:
Code:
1
2
3
4
0
0
0
0
is it possible to get such a result with 'order by' ?
__________________
Lyrics Database: www.shermani.com
Reply With Quote
  #2 (permalink)  
Old 01-21-12, 19:10
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Code:
order by
   case 
     when the_column = 0 then 999999999999
     else the_column
   end
Reply With Quote
  #3 (permalink)  
Old 01-21-12, 19:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
a bit more devious...
Code:
ORDER
    BY ISNULL(NULLIF(the_column,0))
     , the_column
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 01-22-12, 03:25
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Are NULLs always sorted at the top?
Reply With Quote
  #5 (permalink)  
Old 01-22-12, 03:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by shammat View Post
Are NULLs always sorted at the top?
in most database systems, yes

in oracle it's the other way around

now, why doesn't that surprise me?


__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 01-22-12, 04:41
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by r937 View Post
in most database systems, yes
Most databases allow me to control that with "NULLS FIRST" or "NULLS LAST" in the order by clause

Quote:
in oracle it's the other way around
Only for ASC sort, for DESC sort it's the other way round. PostgreSQL, DB2 and Derby behave the same way.

I had to look this up now
According to the standard, it's left to the DBMS on how to behave is neither NULLS FIRST nor NULLS LAST is specified:
Quote:
Originally Posted by SQL Standard
If <null ordering> is not specified, then an implementation-defined <null ordering> is implicit.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On