How to deal with SQL column names that look like SQL keywords?
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;
How to use sql reserve keyword in column name
Use brackets
select [desc] from tablename
how do you insert to a column name that is similar to an SQL statement? in MySQL
These words are called reserved words and in MySQL you will need to escape them using backticks:
insert into yourtable (`condition`)
select yourValue
from yourOtherTable;
My suggestion would be to stay away from using these words for tables and columns.
Selecting a column whose name is a reserved SQL keyword
I fail to see why you need this and I would never use it myself.
declare @T table
(
id int,
name varchar(10),
description varchar(25)
)
insert into @T values
(1, 'kkt', 'kkt description'),
(1, 'skt', 'skt description')
select T2.N.value('*[3]', 'varchar(max)')
from (select *
from @T
for xml path('r'), type) as T1(X)
cross apply T1.X.nodes('/r') as T2(N)
Update
You should do like this instead.
select [desc]
from YourTable
Use []
around column names that is reserved words.
Can't select column name that is a SQL keyword
It works as intended in both scenarios:
CREATE OR REPLACE TABLE example_table("order" INT);
INSERT INTO example_table VALUES (1),(2);
SELECT "order" FROM example_table;
-- 1
-- 2
SELECT e."order" FROM example_table e;
-- 1
-- 2
Most likely there is invisible character used or name is not lowercase as stated in question. When quoting identifier with "
it has to be 1:1 as it was created.
Identifier Requirements
If you put double quotes around an identifier (e.g. “My identifier with blanks and punctuation.”), the following rules apply:
The case of the identifier is preserved when storing and resolving the identifier (e.g. "id" is stored and resolved as id).
CREATE OR REPLACE TABLE example_table("Order" INT);
INSERT INTO example_table VALUES (1),(2);
SELECT "order" FROM example_table;
-- Error: invalid identifier '"order"' (line 17)
SELECT "Order" FROM example_table;
-- 1
-- 2
The "actual" column name could be found using one of the methods described below:
DESCRIBE TABLE example_table;
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ILIKE 'example_table';
SELECT GET_DDL('table', 'public.example_table');
-- create or replace TABLE EXAMPLE_TABLE ( "Order" NUMBER(38,0) );
Escaping keyword-like column names in Postgres
Simply enclose year
in double quotes to stop it being interpreted as a keyword:
INSERT INTO table (id, name, "year") VALUES ( ... );
From the documentation:
There is a second kind of identifier: the delimited identifier or
quoted identifier. It is formed by enclosing an arbitrary sequence of
characters in double-quotes ("). A delimited identifier is always an
identifier, never a key word. So "select" could be used to refer to a
column or table named "select", whereas an unquoted select would be
taken as a key word and would therefore provoke a parse error when
used where a table or column name is expected.
Related Topics
Dplyr Left_Join by Less Than, Greater Than Condition
Sql/MySQL - Select Distinct/Unique But Return All Columns
Cannot Insert Explicit Value For Identity Column in Table 'Table' When Identity_Insert Is Set to Off
How to Get Column Names from a Table in SQL Server
Null in MySQL (Performance & Storage)
Why Isn't SQL Ansi-92 Standard Better Adopted Over Ansi-89
Select N Random Rows from SQL Server Table
Get Top Results For Each Group (In Oracle)
Best Way to Test If a Row Exists in a MySQL Table
Conversion Failed When Converting Date And/Or Time from Character String While Inserting Datetime
How Rownum Works in Pagination Query
How to Get Multiple Counts With One SQL Query
String_Agg For SQL Server Before 2017
Removing Duplicate Rows from Table in Oracle
Select Values That Meet Different Conditions on Different Rows
Error 1045 (28000): Access Denied For User 'Root'@'Localhost' (Using Password: Yes)
What's the Difference Between Not Exists Vs. Not in Vs. Left Join Where Is Null