如何在列名中使用特殊字符选择SQL Server中的列?

时间:2022-08-08 23:45:21

I have a table contain the '%' in the column title, and this cause problem when I do the select statement on that column (Find below for more details). Does anyone know how can I select that column by not keeping the original column title?

我有一个表包含列标题中的'%',当我在该列上执行select语句时,这会导致问题(有关详细信息,请参阅下面的内容)。有谁知道如何通过不保留原始列标题来选择该列?

Example:

例:

Table1
name  ref_no  tot_sales  %Phone
-------------------------------
Alan  1       1          100%
amy   2       1          50%
ken   3       4          30%

Script:

脚本:

Select %Phone From Table1

Error Message :

错误信息 :

Incorrect syntax near phone

电话附近的语法不正确

1 个解决方案

#1


16  

You may want to wrap your column name in square brackets to have your identifier delimited:

您可能希望将列名包装在方括号中以使您的标识符分隔:

SELECT [%Phone] FROM Table1

If the QUOTED_IDENTIFIER option is set to ON, you can also use ANSI-SQL compliant double quotation marks to delimit identifiers:

如果QUOTED_IDENTIFIER选项设置为ON,则还可以使用符合ANSI-SQL的双引号来分隔标识符:

SELECT "%Phone" FROM Table1

#1


16  

You may want to wrap your column name in square brackets to have your identifier delimited:

您可能希望将列名包装在方括号中以使您的标识符分隔:

SELECT [%Phone] FROM Table1

If the QUOTED_IDENTIFIER option is set to ON, you can also use ANSI-SQL compliant double quotation marks to delimit identifiers:

如果QUOTED_IDENTIFIER选项设置为ON,则还可以使用符合ANSI-SQL的双引号来分隔标识符:

SELECT "%Phone" FROM Table1