How to select a column in SQL Server with a special character in the column name?

I have a table containing "%" in the column heading, and this causes a problem when I do a select statement on that column (see below for more details). Does anyone know how I can select this column without preserving the original column header?

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

+7
sql sql-server tsql
source share
1 answer

You might want to wrap your column name in square brackets so your identifier is limited :

 SELECT [%Phone] FROM Table1 

If QUOTED_IDENTIFIER set to ON, you can also use double quotes compatible with ANSI-SQL to delimit identifiers:

 SELECT "%Phone" FROM Table1 
+16
source share

All Articles