One of my columns is called from
. I can't change the name because I didn't make it.
Am I allowed to do something like SELECT from FROM TableName
or is there a special syntax to avoid the SQL Server being confused?
-
6I'd say use ANSI SQL's double quotes for delimited identifiers. It will work on almost any dbms, including SQL Server. I.e. simply do `SELECT "from" FROM TableName`, nice and portable! – jarlh Apr 17 '18 at 09:47
17 Answers
Wrap the column name in brackets like so, from
becomes [from].
select [from] from table;
It is also possible to use the following (useful when querying multiple tables):
select table.[from] from table;

- 107
- 1
- 10

- 524,688
- 99
- 697
- 795
-
12What about: `select TableName.from from TableName;` PS: It works in MySQL – Rudolf Real Sep 10 '12 at 15:09
-
1I tried this just this morning, and it didn't seem to work in my MySQL installation. Is it a parameter or something that turns it on? – CodeChimp Oct 03 '13 at 14:49
-
1@CodeChimp - try using backticks, http://stackoverflow.com/questions/2901453/sql-standard-to-escape-column-names This question/answer is specific to MS SQL Server. – tvanfosson Oct 03 '13 at 15:02
-
Right, but @FabricioPH mentioned it working in MySQL. I happened across here from a Google search after I tried this on my local MySQL install. I was looking to see if there was a generic ANSI SQL way of escaping stuff like this in SQL. We are currently using SQL Server 2005, but we also have Oracle in some of our other apps. We would like to code our Java DAOs in such a way that if we were ever told to move from SQL Server to something else, it would just "work". – CodeChimp Oct 03 '13 at 19:50
-
@CodeChimp I can count the number of times that's happened to me on no fingers. :) – tvanfosson Oct 03 '13 at 22:41
-
@FabricioPH Your solution of writing `tablename.columnname` without the square brackets does not appear to work on SQL Server 2017. Using the square brackets, however, does work. – I Stand With Israel Aug 31 '18 at 18:49
-
In general you'll need brackets around any column names which are the same as SQL Server commands, such as `TableName.[from]`, Israel – Aug 23 '21 at 20:38
If it had been in PostgreSQL, use double quotes around the name, like:
select "from" from "table";
Note: Internally PostgreSQL automatically converts all unquoted commands and parameters to lower case. That have the effect that commands and identifiers aren't case sensitive. sEleCt * from tAblE; is interpreted as select * from table;. However, parameters inside double quotes are used as is, and therefore ARE case sensitive: select * from "table"; and select * from "Table"; gets the result from two different tables.

- 48,070
- 14
- 77
- 93
-
2The double quotes work for MS SQL, too, without the case sensitivity, though. Quoted identifiers are just, AFAIK, an equivalent alternative to bracket-delimited identifiers. – P Daddy Nov 15 '08 at 20:28
-
Double quotes also works for the Presto SQL query engine as used by Amazon's Athena. – Will Humphreys Aug 22 '17 at 17:32
These are the two ways to do it:
- Use back quote as here:
SELECT `from` FROM TableName
- You can mention with table name as:
SELECT TableName.from FROM TableName

- 1,020
- 13
- 12
-
1Thanks, the double quote and square brackets syntax didn't work with my client (MySQLWorkbench) but the back tick method did. – jacob_g Jul 12 '19 at 16:09
While you are doing it - alias it as something else (or better yet, use a view or an SP and deprecate the old direct access method).
SELECT [from] AS TransferFrom -- Or something else more suitable
FROM TableName

- 88,164
- 40
- 182
- 265
Your question seems to be well answered here, but I just want to add one more comment to this subject.
Those designing the database should be well aware of the reserved keywords and avoid using them. If you discover someone using it, inform them about it (in a polite way). The keyword here is reserved word.
More information:
"Reserved keywords should not be used as object names. Databases upgraded from earlier versions of SQL Server may contain identifiers that include words not reserved in the earlier version, but that are reserved words for the current version of SQL Server. You can refer to the object by using delimited identifiers until the name can be changed." http://msdn.microsoft.com/en-us/library/ms176027.aspx
and
"If your database does contain names that match reserved keywords, you must use delimited identifiers when you refer to those objects. For more information, see Identifiers (DMX)." http://msdn.microsoft.com/en-us/library/ms132178.aspx

- 1,969
- 2
- 14
- 19
If you ARE using SQL Server, you can just simply wrap the square brackets around the column or table name.
select [select]
from [table]

- 843
- 1
- 8
- 11
I have also faced this issue. And the solution for this is to put [Column_Name] like this in the query.
string query= "Select [Name],[Email] from Person";
So it will work perfectly well.

- 125
- 12
Hi I work on Teradata systems that is completely ANSI compliant. Use double quotes " " to name such columns.
E.g. type
is a SQL reserved keyword, and when used within quotes, type
is treated as a user specified name.
See below code example:
CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)
--type is a SQL reserved keyword
TYPE
--see? now to retrieve the column you would use:
SEL "type" FROM alpha1

- 641
- 16
- 36

- 31
- 2
Some solid answers—but the most-upvoted one is parochial, only dealing with SQL Server. In summary:
- If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS.
- Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing:
- For SQL Server use [square_braces] around the name. This works in an ODBC connection too.
- For MySQL use `back_ticks`.
- Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.
Dotting the offending word onto the table name may also work.

- 121
- 3
I ran in the same issue when trying to update a column which name was a keyword. The solution above didn't help me. I solved it out by simply specifying the name of the table like this:
UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')

- 1,948
- 23
- 27
The following will work perfectly:
SELECT DISTINCT table.from AS a FROM table

- 10,479
- 11
- 41
- 52

- 21
- 1
Simple solution
Lets say the column name is from ; So the column name in query can be referred by table alias
Select * from user u where u.from="US"

- 2,022
- 22
- 20
In MySQL, alternatively to using back quotes (`), you can use the UI to alter column names. Right click the table > Alter table > Edit the column name that contains sql keyword > Commit.
select [from] from <table>
As a note, the above does not work in MySQL

- 2,006
- 2
- 25
- 33
Judging from the answers here and my own experience. The only acceptable answer, if you're planning on being portable is don't use SQL keywords for table, column, or other names.
All these answers work in the various databases but apparently a lot don't support the ANSI solution.

- 176
- 6
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/28948340) – Tanmay Nehete May 14 '21 at 05:13
-
@TanmayNehete My answer there isn't any link. Either you commented on the wrong answer, or chose the wrong explanation? – David Bradley Jun 04 '21 at 01:00
You can put your column name in bracket like:
Select [from] from < ur_tablename>
Or
Put in a temprary table then use as you like.
Example:
Declare @temp_table table(temp_from varchar(max))
Insert into @temp_table
Select * from your_tablename
Here I just assume that your_tablename contains only one column (i.e. from).

- 5,426
- 9
- 42
- 61

- 11
- 1
-
3That presumes that `[from]` is the only column that `your_tablename` has got. – Andriy M Jan 15 '12 at 17:48
-
What do you gain from the temporary table? It seems completely useless, nothing to do with the question at all. – rjmunro Sep 17 '13 at 11:46
-
@rjmunro, no, this does not seem completely useless. I have a case where I query a tabular cube from SQL and it returns names of columns like '[Total].' That is, the name itself contains '[' and ']'. You can't use [[Total]] and [Total] to retrieve such a column. The easiest way is to put the result of the query into a temp table. – darlove Apr 30 '19 at 10:13
-
@darlove Can't you use quotes: `"[Total]"`? Or maybe there is a way to escape it, something like `[\[Total\]]`? – rjmunro Apr 30 '19 at 10:34
-
@rjmunro, after having experimented with this a bit I've found another way about which I didn't know: you can set QUOTED_IDENTIFIER to ON and then use what you're talking about, the double quote ". So, it's just another way but I wouldn't discard the temp table option completely. – darlove May 01 '19 at 08:43
In Oracle SQL Developer, pl/sql you can do this with double quotes but if you use double quotes you must type the column names in upper case. For example, SELECT "FROM" FROM MY_TABLE

- 31
- 4