What is the difference between square brackets and single quotes for aliasing in SQL Server?

后端 未结 4 2122
花落未央
花落未央 2020-12-28 15:15

I have seen some people alias column names using single quotes eg:

select orderID \'Order No\' from orders

and others use square brackets e

4条回答
  •  温柔的废话
    2020-12-28 15:36

    Single quotes are more readable. As demonstrated above, highlighted in red.

    MySQL uses `backticks` to escape special characters.

    MSSQL can either use "double quotes" or [brackets] for identifiers (tables, columns, etc)
    and 'single quotes' for character strings or aliases.

    The square brackets are used primarily to encapsulate objects so that special characters such as spaces, periods or hyphens do not throw syntax errors.

    I would recommend using the 'as' keyword before your column aliases - it's much more readable.

    select [column with spaces] as 'my col' from "table with spaces" where n = 'foo'
    select "column with spaces" as 'my col' from [table with spaces] where n = 'foo'
    

提交回复
热议问题