How can I select a column named 'date' in Oracle? [duplicate] - sql

This question already has answers here:
How do I escape a reserved word in Oracle?
(5 answers)
Closed 8 years ago.
I have a problem with SELECT query in ORACLE SQL. There is a table named 'Battles' with columns 'name' and 'date'. When I try to do:
SELECT date FROM Battles -
there is an error: (936) ORA-00936: missing expression. I guess the problem is name of column 'date' is similar with datatype name. But how can I deal with it?

Yes you guessed it correct. date is a reserve word in Oracle (in fact it's a datatype) and you should escape it using double quote "" like below.
SELECT "date" FROM Battles
That's the very same reason you should never choose column/table names to be reserve word. Even though almost all the RDBMS provides a way/mechanism to bypass this but it's a never a good practice.

In order to quote a identifier, Oracle uses the double quotes. Be aware, that this also makes them case sensitive (you said the column is named date in lowercase, so):
select "date" from Battles;
See Quoted Identifiers in the Oracle Doc.

Related

Can't execute select query on some columns of table in oracle [duplicate]

This question already has answers here:
View based on apex collection
(2 answers)
Can't use column names in select query on sqlfiddle (oracle)
(3 answers)
Closed 2 months ago.
I have a table with 43 columns. When I execute "Select * from My_Table", it works; And shows data of all columns.
But if I perform "Select" query on some of the columns ( In my case, the first 29 columns of the table)
I receive an error that says "INVALID IDENTIFIER".
Other columns work just fine.
I can't perform "group by" or "order by" using these 29 columns either.
What do you think is the problem?
Any help is appreciated.
Some screenshots are attached for better understanding.
Looking at screenshots, it seems that you created table using mixed letter case and enclosed column names into double quotes. If that's so, well - that's usually bad idea in Oracle as you'll always have to identify columns that way: match letter case and use double quotes.
Therefore, that would be e.g.
select "Order_Id", "Customer_name", "DATA_DATE"
from your_table
Looks awful ... will you remember that customer name doesn't have initial capital letters, but e.g. trace number does?
For you own sake, if possible, drop that table and create a new one as
create table your_table
(order_id number,
customer_name varchar2(20),
trace_number number,
...
);
and reference such a table and columns using any letter case (as it'll work because - by default - Oracle stores names in uppercase (into data dictionary), but lets you reference them any way you want):
select order_id, CUSTOMER_name, TRacE_NumBER ...

SQL Select AS not being recognized as column [duplicate]

This question already has answers here:
Referring to a Column Alias in a WHERE Clause
(9 answers)
Closed 1 year ago.
I am using a simple select statement and creating a column using a CONCAT function and labeling the column as Filter.
Why is the new column not being recognized?
Error message states
Invalid Column Name - Filter
You can't refer to column aliases in the where clause as the where is processed before the alias is materialised.
There are several workarounds, and assuming SQL Server you can do
select *
from table t
cross apply (values(concat(columna,columnb)))c([filter])
where [filter]='something'
Also note that the performance won't be great on large data sets since this criteria is non-sargable

Display row where string column starts with specific letter [duplicate]

This question already has answers here:
ORA 00904 Error:Invalid Identifier
(2 answers)
ORA-00904: "Good Resort": invalid identifier [duplicate]
(1 answer)
Oracle query on String [duplicate]
(1 answer)
Closed 2 years ago.
I researched how to solve my problem, but the compiler I'm using keeps throwing an invalid identifier error. Let me explain.
I have a table with the columns and data types matching respectively
Id | City | State
------------------------
NUMBER | VARCHAR2(21) | VARCHAR(2)
I want to grab all non-duplicate cities which names start with the letter "a". I have tried the below query
SELECT DISTINCT CITY
FROM STATION
WHERE CITY LIKE "A%";
I have also tried
SELECT CITY
FROM STATION
WHERE CITY LIKE "A%"
ORDER BY CITY;
But I keep receiving
ORA-00904: "A%": invalid identifier
I have looked up questions with a similar problem, and the answer seems to integrate the "LIKE" condition. I have done that, but alas, an error. This seems like it should be an easy problem to solve. Any ideas?
Use single quotes! Double quotes are meant for identifiers only (such as column names, table names, and so on), hence the error you are getting.
On the other hand, single quotes stand for literal strings, which is what you mean here:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY LIKE 'A%';
In that regard, Oracle strictly follows the ANSI SQL specification. There are other databases that allow double quotes for strings, and use other characters to quote identifiers (MySQL for example)... but I personally find that it makes things more confusing that they would need to.

Rename a column that is a Reserved Word [duplicate]

This question already has answers here:
How do I escape a reserved word in Oracle?
(5 answers)
Closed 9 years ago.
I have a table called CONTACT_DATA in Oracle, and it has a column called NUMBER. I would like to rename this field to PHONE_NUMBER however, the following does not work because NUMBER is a reserved word in Oracle:
ALTER TABLE CONTACT_DATA RENAME COLUMN NUMBER TO PHONE_NUMBER;
I've looked on these forums, and found how to select and order by a column that is a reserved word. However, in this instance I'd prefer to rename the column instead. Also, I'd like to keep the existing data so dropping the table and re-creating it is not an option. I'm using Oracle version 11.2 Can anyone help?
Have you tried:
ALTER TABLE CONTACT_DATA RENAME COLUMN "NUMBER" TO PHONE_NUMBER;
Oracle uses the double quotes as an escape character for this purpose.

Select statement don't work with double quotation in Oracle [duplicate]

This question already has answers here:
How to deal with SQL column names that look like SQL keywords?
(17 answers)
Closed 10 years ago.
My table has column called select (or NOT, or other reserved keys).
I tried select 'select' column but I couldn't because oracle throw exception like this:
invalid user.table.column or table.column or column specification
My select statement like this:
select f.select
from foo f.
I was searching internet and I found accepted answer: How do I escape a reserved word in Oracle?
then, I changed my query: Select "f"."select" from foo f, but throw exception too like this:
"c"."select": invalit identifier.
How to use "select" column in select (or update, insert) statement?
P.S: I'm using pl/sql developer tool for querying data.
Try,
Select f."select" from foo f;
When identifiers are quoted in Oracle, they become case sensitive.
So it's likely that you have to write the following:
select f."SELECT" from foo f
(or "Select", or however you've actually written the column name)
This should work fine:
Select f."select" from foo f