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

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

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 ...

Column doens't exists in PostgreSQL (WHERE column_name = column_value) [duplicate]

This question already has answers here:
delete "column does not exist"
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Column 'mary' does not exist
(2 answers)
postgres - where in (list) - column does not exist
(2 answers)
Closed 2 years ago.
I have the following table in PostgreSQL:
id and grade are INTs, note and subject both VARCHARs
When I run the command:
SELECT * FROM grades
WHERE subject = "latin";
I get the following error:
In pgAdmin4:
ERROR: column "latin" does not exist
LINE 2: WHERE subject = "latin"
^
SQL state: 42703
Character: 37
And in cmd:
ERROR: column "latin" does not exist
LINE 1: SELECT * FROM upisi WHERE subject = "latin";
I'm coming from MySQL so I thought this would work.
Works fine if I put grade = something in the WHERE clause. Any idea why this might be the case?
You need single quotes for character constants. (double quotes are used to quote identifiers)
SELECT * FROM grades
WHERE subject = 'latin';
If you use WHERE subject = "latin", the DBMS expects "latin" to be a column name, which it is not.
It's as simple as the wrong type of quote marks. You wanted:
SELECT * FROM grades
WHERE subject = 'latin';
To explain:
Single quotes, like 'latin', are the standard way to write a string in standard SQL, and should work on all DBMSes.
Double quotes, in Postgres and some other DBMSes, are a way of quoting identifiers - so if your column name had a space in for some reason (there's not many good reasons to do it, but it's possible), then you might write SELECT * FROM grades WHERE "subject name" = 'latin' - "subject name" is the name of the column, 'latin' is a string.
Although double quotes are in the SQL standard, other DBMSes use different syntax for quoting identifiers, so may treat double quotes as an alternative for writing strings.
-- Postgres (also works in Microsoft SQL Server, but isn't the default style)
SELECT * FROM grades WHERE "subject name" = 'latin'
-- MySQL
SELECT * FROM grades WHERE `subject name` = 'latin'
-- Microsoft SQL Server
SELECT * FROM grades WHERE [subject name] = 'latin'
But if you always use single quotes for strings, and avoid names that need quoting, you'll run into fewer problems.
-- Works pretty much everywhere
SELECT * FROM grades WHERE subject = 'latin'

Select a word from an Oracle query string using regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have to find a table name from a query dynamically. The table name in the query may or may not be preceded by the schema name. But the table name will always be followed by a "#dblink" string.
The query pattern is like : 'select c1, c2 from schema.table_name#dblink ...'
Or, it could be like : 'select c1,c2,c3 from table_name#dblink ...'
That is, the number of columns - c1, c2 etc - are variable. Also, the developers can probably put in multiple consecutive spaces anywhere between any 2 words.
My objective is to retrieve the name of the table "table_name".
How can I do this using regular expression ?
If you want the word that preceds '#dblink', you can do:
regexp_replace(mycol, '.*\W(\w+)#dblink.*', '\1')
declare var
SELECT name into var from table where name LIKE '%#dblink';
You can print like this
DBMS_OUTPUT.PUT_LINE (var);

Postgresql escape single quote in where clause [duplicate]

This question already has answers here:
Insert text with single quotes in PostgreSQL
(8 answers)
Closed 3 years ago.
so I am trying to run a script like this one:
select id
from owner
where owner.name = "john's"
and I am getting this error: ERROR: column "john's" does not exist.
Also I tried like this: where owner.name = 'john\'s', but it dit not work
Anyone knows how I can run a query like this one?
You can escape single quotes when you double them.
For example:
= 'john''s'
Try this
select id
from owner
where owner.name = (E'john\'s')::text
Update:
we can escape most of the characters using this statement
select id
from owner
where owner.name = (E'john\character you want to escape's')::text

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

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.