[] Brackets in SQL Statements

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

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.

Meaning of square brackets [] in MS-SQL table designer?

The square brackets [] are used to delimit identifiers. This is necessary if the column name is a reserved keyword or contains special characters such as a space or hyphen.

Some users also like to use square brackets even when they are not necessary.

From MSDN:

Delimited identifiers

Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers may or may not be delimited.

SELECT *
FROM [TableX] --Delimiter is optional.
WHERE [KeyCol] = 124 --Delimiter is optional.

Identifiers that do not comply with all of the rules for identifiers must be delimited in a Transact-SQL statement.

SELECT *
FROM [My Table] --Identifier contains a space and uses a reserved keyword.
WHERE [order] = 10 --Identifier is a reserved keyword.

[] brackets in sql statements

The [] marks the delimitation of a identifier, so if you have a column whose name contains spaces like Order Qty you need to enclose it with [] like:

select [Order qty] from [Client sales]

They are also to escape reserved keywords used as identifiers

Are brackets in the WHERE clause standard sql

Yes. You can use parenthesis to bind components of where clauses. This isn't necessary in your example, but if you had multiple and and or components, you might need parenthesis to either ensure correct order of operations or simply to self-document the query.

Example 1:

select *
from foo
where
(class='A' and subclass='B')
or (class='C' and subclass='D')

In example 1, the parens aren't strictly required because and binds more tightly than or, but if you had multiple or conditions tied by and you would need it to get correct results, as in example 2 below.

Example 2:

select *
from foo
where
(class='A' or class='B')
and (subclass='C' or subclass='D')

I use them in either case, because I don't like having to parse the sql in my head the same way the query optimizer does -- I'd rather be explicit about it and more quickly understand what the intent is.

What do brackets mean around column names in SQL Server?

Columns enclosed in square brackets are usually keywords or contain special characters or spaces.

What specific column name do you have enclosed in brackets?

What is difference in SQL Server between [] and ' '

Single quotes denote a string literal, while square brackets ([..] - SQL Server/T-SQL specific) or double quotes (ANSI/ISO SQL standard) can enclose table (or column) names with spaces ("Order Items"), or names that start with a numeric (instead of a character - like [1998 Sales]).

So you should also be able to use this:

SELECT *
FROM "Order Details"
WHERE OrderID = 11077

sql select value with square bracket in Like clause?

Searching with special characters

SELECT * 
FROM ABC
WHERE title LIKE N'%\[text] xxxxx%' ESCAPE '\'

What means square brackets in Oracle SQL query?

According to oracle's documentation: http://docs.oracle.com/cd/B10500_01/text.920/a96518/cqspcl.htm

The bracket characters serve to group terms and operators found between the characters; however, they prevent penetrations for the expansion operators (fuzzy, soundex, stem).

Its a grouping character in the query.



Related Topics



Leave a reply



Submit