Can we enclose column name with double quotes in queries like select query

Pavan G 0 Reputation points
2025-01-06T12:18:11.1333333+00:00

Hey Team,

I'm dealing with multiple teams applications sql server database every day.

I'm getting different types and combinations of column names with space, special characters etc.

So that's why my query uses square brackets to enclose column names as below

select [column name1] from [schema].[table1]

But I have a column itself contains square brackets in its name, so my query will become like below

select [[column name1]] from [schema].[table1] - which throws error multiparty identifier error

If we change it to square brackets select "[column name1]" from [schema].[table1]

It will work with no issue

My concern is can we use square brackets to enclose column name of any kind. Does this recommended to use officially?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,282 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,304 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 45,391 Reputation points
    2025-01-06T12:21:54.17+00:00

    Test it with

    select quotename('[Test it]')
    
    

    returns

    [[Test it]]]
    
    0 comments No comments

  2. Erland Sommarskog 115.1K Reputation points MVP
    2025-01-06T12:57:19.7566667+00:00

    You can use both double quotes and square brackets to enclose identifiers that contain character that is not part of the regular syntax for identifiers.

    The double quotes is the ANSI standard, and should work on many database engines. The square brackets, on the other hand, are proprietary and may only work on SQL Server and Access (from which I believe the origin.)

    In SQL Server, the double quotes only serves for quoting identifiers if the setting QUOTED_IDENTIFIER is ON. This is the default setting in most contexts, and you should always keep it ON. OFF is purely a legacy setting. There are some situations where QUOTED_IDENTIFIER for stupid reasons defaults to OFF. One is SQL Server Agent, but it is not a major problem as long as you are running stored procedures. This is because for a stored procedure, this is not a run-time setting, but it saved with the procedure.

    If you have names with brackets, I definitely think that you should use the double quotes to delimit them, so that the code gets more readable.

    My own personal preference these days is for the double quote. Although the reason for this is quite a trivial one: It's easier to achieve on a Swedish keyboard.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.