SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Proper type of formatting for AS Clause? (Now and in the future?)


Proper type of formatting for AS Clause? (Now and in the future?)

Author
Message
SQL Knight
SQL Knight
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 34
All 3 of these AS Clause below will work in MS T-SQL.
But my question is which on is more proper type of formatting for T-SQL (Now and in the future of later version of MS SQL?)



---- Sample 1
SELECT 'Hello' AS 'Greeting';

---- Sample 2
SELECT 'Hello' AS Greeting;

---- Sample 3
SELECT 'Hello' AS [Greeting];





I'm looking for the real reason why one way is better than other?
Ok we all know the [] and '' will allow you to do a space in column names. (A small benefit) Like AS [Today Greeting] or AS 'Today Greeting'
But I would like to know if one way is possible a faster performance/or less back end resources of SQL engine behind the real code.

Maybe this might be a silly/dumb question, but does anyone know the real answer and can prove it?
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52540 Visits: 21181
SQL Knight (4/18/2013)
All 3 of these AS Clause below will work in MS T-SQL.
But my question is which on is more proper type of formatting for T-SQL (Now and in the future of later version of MS SQL?)



---- Sample 1
SELECT 'Hello' AS 'Greeting';

---- Sample 2
SELECT 'Hello' AS Greeting;

---- Sample 3
SELECT 'Hello' AS [Greeting];





I'm looking for the real reason why one way is better than other?
Ok we all know the [] and '' will allow you to do a space in column names. (A small benefit) Like AS [Today Greeting] or AS 'Today Greeting'
But I would like to know if one way is possible a faster performance/or less back end resources of SQL engine behind the real code.

Maybe this might be a silly/dumb question, but does anyone know the real answer and can prove it?




In terms of the SQL engine and performance, I doubt that there is any difference. So I apologise in advance if this is not what you are interested in reading.

The differences are mostly about readability and style.

First of all, 'AS' is a readability word when it comes to aliasing in T-SQL - it can be omitted completely. I prefer my SQL to be as concise as possible, so I always omit it.

The square brackets are required in a couple of cases that I can think of:

1) Where your chosen alias contains a space
2) Where your column alias is a reserved word in T-SQL

Each of the above is generally considered bad practice, so brackets can usually be omitted. For the same reason as mentioned above, I omit them if possible.

Putting your alias name in single quotes means that the colour coding in SSMS makes things confusing (to my eyes anyway) because it makes the aliases appear the same as literal text.

My final comment - my personal preference is to use the following style:

SELECT Greeting = 'Hello',
Greeting2 = 'Good day';



Working this way with long SELECT statements makes it easier to see all of the column names in the resultset, because they all line up rather than appearing raggedly on the right.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search