Friday, 12 October 2012

SQL Server ORDER BY Varchar aka Text Field, The Easy Way

When trying to order by a varchar field you can use the following syntax to create a specific order. You can add as many fields as you require to this in the order you wish. Simply add this to end of your statement:

ORDER BY CASE (MyLevel)
WHEN 'Intermediate' THEN 1
WHEN 'Advanced' THEN 2
WHEN 'Higher' THEN END

In the above example I have a field called MyLevel which is a varchar(50) field which can have 1 those 3 values.

Another annoyance solved hopefully :)

No comments: