Click here to Skip to main content
Click here to Skip to main content

Order by Clause with Column_Index instead of Column Name

By , 18 Jul 2011
 
Its a very general and regular ways to use Order by clause with Column Name to Specifies the sort order used on columns returned in a SELECT statement.
Syntax :-
order by columnname asc/desc

I am also using the same method but do you know we can use Column Index (Integer representing the position of column name) instead of specifying the name or column alias in a SQL Server Order By expression.
As:-
order by Column_Index_Number asc/desc
we can use both queries because both of these queries having the same results.
 
let check both syntax and result too
here I am going to query based on AdventureWork database
 
Example :- Getting top 10 value from Employee table based on EmployeeID in asc order
select top  10 * from HumanResources.Employee order by EmployeeID asc
output will be as given below :-

 
when Using Column Index instead of Column Name for same query
Note :- Column Index can be changed as 1,2,3,etc based on column name condition
select top  10 * from HumanResources.Employee order by 1 asc
then output will be same as given below :-

 
Reference Link :- Matt Berseth Articles[^]
 
Note :- ORDER BY clause is only Clause where we can you use the ordinal position[Column Numbers], because it's based on the column(s) specified in the SELECT clause.
its generally recommended that to use Column Name instead of Column Number. but in some cases using Column Number can be useful like it can be used it in a dynamic sql where column names are unknown.
for getting practical way you can follow this[^] blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RaviRanjankr
Software Developer
India India
Member
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralIts good to know but this not making any difference on execu...memberam.net25 Jul '11 - 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 18 Jul 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid