Sunday, March 2, 2008

Custom ORDER BY in SQL

.NET provides very useful techniques for custom ordering, e.g. generic comparison delegate, lambda expressions, IComparable interface etc.

When the sort order is needed at database layer, you can use the following statement:

SELECT *

FROM ContestParticipants

ORDER BY

CASE result

  WHEN 'winner' THEN 1

  WHEN 'loser' THEN 2

  WHEN 'dnf' THEN 3

  ELSE 4

END



kick it on DotNetKicks.com