SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    united states
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displaying empty strings last when using order by

    Hello

    I would like to be able to display the content of a table sorted alphabetically (ascending) by 'name' (one of the columns) with the following little twist:
    all the rows which have an empty name column should be displayed last. The rest of the rows should be displayed before them with regular ascending alphabetical order.

    I also have the requirement that I must use limit x,y on the query so I will not extract the entire table.

    how can I combine all these requirements into a single query?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select foo
         , bar
         , name
      from yourtable
    order 
        by case when length(name) > 0 
                then 0 else 1 end
         , name   
    limit x,y
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •