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
How to Concatenate Numbers and Strings to Format Numbers in T-Sql
How to Create a Date in SQL Server Given the Day, Month and Year as Integers
How to Count the Number of Occurrences of a Character in an Oracle Varchar Value
How to Remove Time from Datetime
Oracle - How to Create a Materialized View with Fast Refresh and Joins
Update a Column of a Table with a Column of Another Table in Postgresql
Ad Hoc Queries VS Stored Procedures VS Dynamic SQL
Postgres Error: More Than One Row Returned by a Subquery Used as an Expression
Check If Current Date Is Between Two Dates Oracle SQL
How to See All the Tables in an Hsqldb Database
Lightweight SQL Database Which Doesn't Require Installation
Use a Union or a Join - What Is Faster
Selecting Top N Rows for Each Group in a Table
How to Use My SQL Knowledge with Cloudant/Couchdb
Can an Inner Join Offer Better Performance Than Exists
How to Convert Postgresql 9.4's JSONb Type to Float