30

I added a field in SQL Server 2008 and it put the column name in brackets like this: [column2]. The other column names don't have brackets. What do the brackets mean?

The column is named macro-writer

Should I remove the minus sign and replace it with an underscore?

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • 2
    OP said `should i remove the minus sign and replace with underscore`. I would, if only so I don't have to use `[` and `]` everywhere! – KM. Aug 23 '10 at 20:26
  • It'll try to perform an arithmetic operation of `macro-writer` and will throw invalid column name exceptions for `macro` and `writer`. – Lukas G Jun 15 '20 at 19:37

5 Answers5

33

Brackets are a form of quoting. It's only necessary if the column name contains spaces or punctuation or conflicts with a reserved word, but many wizards will just add the brackets for all field names to avoid the logic for deciding whether they are necessary.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
27

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

What specific column name do you have enclosed in brackets?

Bernard
  • 7,908
  • 2
  • 36
  • 33
9

Brackets allow you to use characters and names which are not allowed like spaces, reserved words and names starting with numbers

invalid my column, 1column col%umn, table

valid [my column], [1column], [col%umn], [table]

of course now you can become really creative :-)

create table [table]([table] varchar(20))

insert [table] values ('table')

select [table] from [table]
SQLMenace
  • 132,095
  • 25
  • 206
  • 225
  • you will never have a table that has a mix of hebrew and thai font – Alex Gordon Aug 23 '10 at 20:42
  • 1
    Well, why not try it and see for yourself ;). Works on SQL 2005, 2008 create table [แสดงבוקרǣ] ( ID int not null ) – StuartLC Aug 25 '10 at 07:49
  • 1
    Select [table] from [table] where [table].[table] = 'table'. I love it!! – sMaN Dec 07 '15 at 23:39
  • I think that using such spaces and special characters for a column identifier could just be making unnecessary trouble for onesself elsewhere and generally avoid it. But what is the advantage of using `[my column]` over the more traditional SQL `"my column"`? – Ed Randall Nov 12 '21 at 06:57
4

See related questions with answers:

I would recommend using underscore instead of dash in identifier names.

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
3

Brackets allow you to delimit names in SQL Server. This allows you to do such things as use keywords [count] or include spaces [my column name].

EDIT: For your follow up question, if this is a new column and there's no risk of breaking existing code, then I'd at least recommend replacing the hyphen with an underscore. Our own internal naming standards are to use PascalCase (e.g., MacroWriter) rather than underscores.

Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235