Square braces in SQL Server play a major role in T-SQL programming. When an object name contains a space, special character, etc, the only way to express them is to put them around squre braces. Consider that you want to create a table user master (with spaces between user and master), you can use [user master].
You can also wrap alias names in square braces
select 1[74]
The result is
74
-----------
1
Ok. Now I want to have [74 as column alias. What should I do? I can simply put one more opening square brace like below
select 1[[74]
which results to
[74
-----------
1
What if I want to have [74] as column alias? Simply use one more closing square brace. Isn't it? Well the following code
select 1[[74]]
throws this error
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string '[74]'.
Ok. Now can you guess the output of the following statement?
select 1[[74]]],1[[[[[74]]]]]]]]],1[[[[74]]]]]]]
The output is
[74] [[[[74]]]] [[[74]]]
----------- ----------- -----------
1 1 1
Why are there more number of closing braces than the opening braces used in the statement to produce the output that has same number of opening and closing braces?
The answer is that the last sqaure brace indicates the end of the square braces wrapping. Other than the last square brace, every double closing braces is replaced as single closing brace in the output alias names. But for opening braces, the output is one less than the total number. The first opening braces indicates the beginning of the wrapping. So you need to be aware of these when using multiple square braces in the column alias.