Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    124

    Unanswered: ORDER BY Canada, Unisted States then

    In a country list I would lint the order to be: ORDER BY Canada, United States,
    then the rest of the countries alphabetical. Would I need to do two UNIONs?

    Nick?

  2. #2
    Join Date
    Nov 2003
    Posts
    2,933
    Provided Answers: 12
    No

    Code:
    ORDER BY 
        CASE 
           WHEN country_name = 'Canada' THEN 'AAAAAA'
           WHEN country_name = 'United States' THEN 'AAAAAB'
           ELSE country_name
        END

  3. #3
    Join Date
    Jan 2009
    Posts
    124
    I didn't realize CASE could be used except in a SELECT statement! Can it be used in the WHERE?

    Thanks
    Nick

  4. #4
    Join Date
    Jan 2009
    Posts
    124
    So if I have list of countries and I want one or two of the countries a the top and the rest alphabetically, this will do it?

    Nick

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by oldnickj View Post
    ... this will do it?
    what happened when you tested it?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Jan 2009
    Posts
    124
    OK, your right, it works!
    CASE is a lot more interesting than I thought.

    Nick

Posting Permissions

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