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

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.


They're handy if your columns have the same names as SQL keywords, or have spaces in them.

Example:

create table test ( id int, user varchar(20) )

Oh no! Incorrect syntax near the keyword 'user'.But this:

create table test ( id int, [user] varchar(20) )

Works fine.


They are useful if you are (for some reason) using column names with certain characters for example.

Select First Name From People

would not work, but putting square brackets around the column name would work

Select [First Name] From People

In short, it's a way of explicitly declaring a object name; column, table, database, user or server.