Order by with a particular value on TOP or Custom sorting in order by clause.
ORDER clause can be used to sort the results returned by SELECT statement in SQL Server. It orders the result set by specified column list. When used with character data type columns it sorts data in dictionary-order.
Here i am writing related sql from which we can set particular value on top and remain list in sorted format .Take one case some time we have need to display desired country name on top and remain in sorted form .
Consider Following list of country :-
country_name
Bangladesh
Cambodia
India
Pakistan
Bahrain
China
Spain
Sri Lanka
Namibia
Sql query :-
SELECT * FROM country_master order by case when country_name=’India’ then null else country_name end asc;
here country_master is database table name and country_name is column name in table . in this example India will be on top and remain will be in sorting form . result will be look like this .
Result :
India
Bahrain
Bangladesh
Cambodia
China
Namibia
Pakistan
Spain
Sri Lanka
As you can see from the results above, both results are now in desired position, while remaining values are sorted in a standard order.
Recent Comments