An issue with specifying a column "Percent" in SQL Select query

Viewed 610

There is a table with a column name PERCENT. I need to run an ordinary SELECT query:

SELECT * 
FROM <my_table>
WHERE PERCENT = 100

However, I get this error message:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'PERCENT'

Is there a way to indicate a column so the system accepts it as a column name instead of a function?

2 Answers

PERCENT is a reserved word. Use escape characters:

SELECT *
FROM <my_table>
WHERE [PERCENT] = 100

It is used, for instance, with TOP as in TOP 100 PERCENT.

Enclose the column name in square brackets (i.e. [PERCENT]) or the ANSI/ISO standard double quotes (i.e. "PERCENT"). This will allow you to use reserved T-SQL keywords in identifiers.

I suggest one avoid using reserved keywords as object or column names to avoid this requirement.

Related