How to Deal With SQL Column Names That Look Like SQL Keywords

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



Leave a reply



Submit