299

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?

1
  • 6
    I'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, 2018 at 9:47

17 Answers 17

438

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;
7
  • 12
    What about: select TableName.from from TableName; PS: It works in MySQL Sep 10, 2012 at 15:09
  • 1
    I 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 3, 2013 at 14:49
  • 1
    @CodeChimp - try using backticks, stackoverflow.com/questions/2901453/… This question/answer is specific to MS SQL Server.
    – tvanfosson
    Oct 3, 2013 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 3, 2013 at 19:50
  • @CodeChimp I can count the number of times that's happened to me on no fingers. :)
    – tvanfosson
    Oct 3, 2013 at 22:41
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.

2
  • 2
    The 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, 2008 at 20:28
  • Double quotes also works for the Presto SQL query engine as used by Amazon's Athena. Aug 22, 2017 at 17:32
27

These are the two ways to do it:

  1. Use back quote as here:

SELECT `from` FROM TableName

  1. You can mention with table name as:

SELECT TableName.from FROM TableName

1
  • 1
    Thanks, the double quote and square brackets syntax didn't work with my client (MySQLWorkbench) but the back tick method did.
    – jacob_g
    Jul 12, 2019 at 16:09
22

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
12

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

0
9

In Apache Drill, use backquotes:

select `from` from table;
8

If you ARE using SQL Server, you can just simply wrap the square brackets around the column or table name.

select [select]
from [table]
4

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.

3

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
1
  • I confirm this works in DBeaver with Teradata database, thanks!
    – Paul
    Aug 31, 2019 at 0:11
3

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.

2

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?')
2

The following will work perfectly:

SELECT DISTINCT table.from AS a FROM table
2

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"
1

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
  • 3
    That presumes that [from] is the only column that your_tablename has got.
    – Andriy M
    Jan 15, 2012 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, 2013 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, 2019 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, 2019 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 1, 2019 at 8:43
1

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

1

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.

2
  • 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 May 14, 2021 at 5:13
  • @TanmayNehete My answer there isn't any link. Either you commented on the wrong answer, or chose the wrong explanation? Jun 4, 2021 at 1:00
0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.