Monday, July 26, 2010

MS SQL Column Alias

T-SQL used in Microsoft SQL Server seem to have very limited support for column alias. If you have a complex calculated column that you want to re-use in GROUP, SELECT or another expression, it will be desirable to use alias to reference the same definition everywhere so that we can have a clear and easier to understand SQL statement.

Here is an article that is very helpful in this matter: Avoid Transact-SQL's Column Alias Limitations

Key points:
  • MS SQL only allow alias in SELECT and ORDER BY
  • Use sub-query, can help in some cases to re-use column alias

No comments: