3

What is actually difference between [] and ' ' I'm asking because when I wrote the query I couldn't write

SELECT *
FROM 'Order Details' 
WHERE OrderID = 11077

only possibility was

SELECT *
FROM [Order Details] 
WHERE OrderID = 11077

but I can rename AS 'some name for column' why ' ' is sometimes wrong?

I'm using the Northwind sample database.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

3

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

Avoid spacess in table/column name. Brackets are required, when in table name have space, Sqlserver consider it 2 different word.

When we use reserved word or string as a column / table name, we required square brackets.

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.

Please see the same question's answer:

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

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

https://dba.stackexchange.com/questions/21779/sql-server-2012-is-putting-brackets-around-table-and-column-names

Community
  • 1
  • 1
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
0

This is example

select [first Name], [last name] from tablename where [last name]='name'

[] is used to add a space between two words in column like in above [last name]

and

'' is used for a strings or execute the query as a string like 'name' and also using in full query in parameter like

declare @perameter1
set @perameter= ('select * from tablename where name like =''name''')