Custom sort order (ORDER BY&CASE)

  • Hi. Recently I've found the following query:

    USING Northwind  

    SELECT *

    FROM  Customers

    ORDER BY

      CASE Country

      WHEN 'Canada' THEN char (1)

      WHEN 'USA' THEN char (2)

      WHEN 'Mexico' THEN char (3)

      ELSE Country

      END,

      Region

     

    I know what does this query do, but don't know exactly "how". Could someone explain it to me ?

    Thank U in advance.

  • Well, when you sort letters like 'abc', you don't really sort by the letters, you use the configured ascii table to find which numbers that 'abc' correspond to, then you sort. So, 'abc' is from the computers view 979899.

    CHAR(1) is the same as saying ASCII code #1 (which is some weird control char, but it's #1, so it'll always come first)

    Enough explained?

    /Kenneth

  • Thanks for Your explantion. I knew that it's assotiated with ASCII codes, but I didn't get the idea before writting my post

    thx a lot

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply