• 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.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.