Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

select orderID 'Order No' from orders 

and others use square brackets eg:

select orderID [Order No] from orders 

I tend to use square brackets. Is there any preference/difference?

like image 956
Free2Rhyme2k Avatar asked Oct 29 '13 11:10

Free2Rhyme2k


People also ask

What is the use of the square brackets [] in SQL statements?

On SQL Server and MS Access, square brackets have a special meaning when used in a query filter. The square brackets are used to specify a set or range of characters, as in "[A-Z]" which would match any single character from 'A' to 'Z'.

Why do you put [] in SQL?

The brackets are required if you use keywords or special chars in the column names or identifiers. You could name a column [First Name] (with a space) – but then you'd need to use brackets every time you referred to that column. The newer tools add them everywhere just in case or for consistency.

What does [] mean in SQL Server?

the brackets are special characters in sql server that are used to explicitly delimit information. they can be used in xml as per the article, they can also be used to specify meta names (column, table, etc.)

Do you need square brackets in SQL?

1) If you have SQL keyword, space or any other illegal characters then you need to use square brackets.


2 Answers

To answer the question "is there any preference/difference":

Yes, there are as many preferences as there are opinions, but be careful whose preferences you adopt.

As a best practice, it is advisable to write portable SQL if it doesn't require any extra effort.

For your specific sample, it is just as easy to write a portable query...

select OrderId as "Order Id" from Orders 

... as it is to write a non-portable one:

select OrderId as [Order Id] from Orders 

It is preferable not to write non-standard SQL when there is an equivalent portable form of the same number of keystrokes.

The proliferation of [] for escaping is due to tools like SQL Server Management Studio and MS Access query builders, which lazily escape everything. It may never occur to a developer who spends his/her career in SQL Server, but the brackets have caused a lot of expense over the years porting Access and SQL Server apps to other database platforms. The same goes for Oracle tools that quote everything. Untrained developers see the DDL as examples, and then proceed to use the same style when writing by hand. It is a hard cycle to break until tools improve and we demand better. In Oracle, quoting, combined with mixed casing, results in case sensitive databases. I have seen projects where people quoted every identifier in the database, and I had the feeling I was in The Land of The Lost where the developers had evolved on an island without documentation or best practice articles.

If you write your DDL, from the start, with normalized, legal identifiers (use OrderId, or order_Id instead of [Order Id], you don't worry about the mythical keyword that might need escape characters; the database will inform you when you've used a reserved word. I can count on one finger the times we've ever upgraded an app from one version of SQL Server to another and had any breakage due to new reserved words.

This is often the subject of heated debate, so if you think about it another way:

C# programmers don't escape all their variables with @, even though it is legal to do so. That would be considered an odd practice, and would be the subject of ridicule on StackOverflow. Escaping should be for the edge cases. But the same developers that write conforming C# identifiers don't mind escaping every single identifier in their SQL, writing terribly ugly, non-portable SQL "code". As a consultant, I've met more than one SQL Server programmer who honestly thought [] was required syntax. I don't blame the developers; I blame the tools.

like image 105
codenheim Avatar answered Oct 23 '22 07:10

codenheim


It depends on what settings you have in force whether 's are valid or not. And you missed out ". See Delimited Identifiers:

When QUOTED_IDENTIFIER is set to ON, SQL Server follows the ISO rules for the use of double quotation marks (")and the single quotation mark (') in SQL statements. For example:

  • Double quotation marks can be used only to delimit identifiers. They cannot be used to delimit character strings.

  • Single quotation marks must be used to enclose character strings. They cannot be used to delimit identifiers.


When QUOTED_IDENTIFIER is set to OFF, SQL Server uses the following rules for single and double quotation marks:

  • Quotation marks cannot be used to delimit identifiers. Instead, brackets have to be used as delimiters.

  • Single or double quotation marks can be used to enclose character strings.

And finally:

Delimiters in brackets can always be used, regardless of the setting of QUOTED_IDENTIFIER

Where, in all of the above quotes, when they refer to brackets they're talking about [] brackets.

like image 20
Damien_The_Unbeliever Avatar answered Oct 23 '22 06:10

Damien_The_Unbeliever