question

user-470 avatar image
user-470 asked

Difference between SELECT Alias = ColumnName and SELECT ColumnName AS

I have noticed two different approaches to specifying the name of a column in a resultset differently from the table column name. The one I am most familiar with is of the form SELECT ColumName as AliasName FROM TheTable But I am now quite frequently seeing SELECT AliasName = ColumnName FROM TheTable From what I can tell, these behave in exactly the same way, but I am curious if there are, in fact, any subtle differences, or an advantage with one approach over the other?
sql-server-2008select
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image
Grant Fritchey answered
Overall, there's no real difference, but since the equals sign is for assignment or comparison, I leave it at that.You can't alias a table with an equals sign, so using it for column aliases just feels weird. I alias using AS.
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
I too use "ColumnName as AliasName" as I believe it to be much cleaner. Just personal preference though.
0 Likes 0 ·
user-470 avatar image user-470 commented ·
Do you know if both forms are ANSI compliant?
0 Likes 0 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
@user-470 I'm not sure. I just went to the ANSI standard that is available and I can't find any reference to this at all.
0 Likes 0 ·
klauskowarsch avatar image
klauskowarsch answered
I am one of the early promoters of Alias = column name OR Alias = formula (circa 1999-2000). For me, it is about legibility (which increases understanding and speed). The SQL code that you write is read far more frequently than it is written/rewritten. And even before it can be rewritten it must be read. Usually, when we as developers are reading something it is to reverse engineer it and/or to problem solve starting with an issue that occurred downstream such as the report / UI layer. If your code is as simple as Alias/column name then the difference is "six and half a dozen of the other". But often when aliasing something it is because their is a transformation/calculation. Case statements and calculations can get UGLY! So to the point made above rationalizing his preference for using ColumnName as Alias. **If you are doing string manipulation or some sort of formula or calculation than isn't that the same as assignment?** **Usually** when I look at a stored procedure or other SQL code, **I do not need or want to read every single case statement and formula for my assigned task**. If my alias come first and the ugly formulas and calculations are indented properly, I only need to read what I need. Again personal preference. If your code is ugly I WILL REFORMAT. if it poorly performs, I will rewrite it. If not I may leave everything the same except what I need to change.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.