How to SELECT COL_"NAME" in Snowflake ? (COLUMN name containing double quote) - sql

I know this is very bad naming practice, but I am not the owner of the table...
I need to run :
SELECT COL_"NAME"
FROM TABLE
where COL_"NAME" is the name of the column, containing double quotes.
I tried :
SELECT COL_""NAME""
FROM TABLE
SELECT COL_\"NAME\"
FROM TABLE
But nothing works

Identifier Requirements:
To use the double quote character inside a quoted identifier, use two quotes.
To access column: COL_"NAME"
SELECT "COL_""NAME"""
FROM TABLE;

Related

Snowflake column with triple double-quotes is always flagged as "Invalid identifier"

Multiple snowflake columns are like this """SOME TEXT WITH SPACES"""
No issue when I'm doing a SELECT * FROM MY_TABLE
But I can't figure out how to query the columns with double-quotes...
If I do :
SELECT """SOME TEXT WITH SPACES""" FROM MY_TABLE
I receive
SQL compilation error: error line 1 at position 7 invalid identifier '""SOME TEXT WITH SPACES""'
I've tried adding other double-quotes but I can't find the right combination...
If you have a table when selected from:
select * from names;
looks likes this:
NO_QUOTE
also_no_quote
"single_quotes"
"""triple_quotes"""
0
0
1
3
You need to understand how it was created, which is also how it need to be accessed :
There is the first layer of double quotes to turn off the case insensitivity.
Then for each extra layer of wanted double quotes in the output you have to use two double quotes on both sides.
Thus tripple quotes was made and accessed by 7 double quotes:
create or replace table names (no_quote int,
"also_no_quote" int,
"""single_quotes""" int,
"""""""triple_quotes""""""" int);
insert into names values (0,0,1,3);
and thus can be accessed by:
select no_quote, "also_no_quote", """single_quotes""", """""""triple_quotes""""""" from names;
You need to select the column name using quotes because column names are case sensitive if they are created in double quotes.
Example
create or replace table doublequotes (
seq int,
"""last_name""" string,
first_name string
);
insert into doublequotes values (10, 'abcd', 'efgh');
select """last_name""" from doublequotes;
o/p: abcd
Please try.

PostgreSQL where clause condition check with value that contains single quote

Suppose I have an employees table in Postgres DB where I have to insert a value for an employee name which is john's
Since it's Postgres I will escape the single quote ' by doubling them up -> ''
So john's will become john''s
Now when I select that particular row/instance using select query I have to double the quote again. So to select the value john''s I have to write 'john''''s' and my query becomes -
select * from employees where name = 'john''''s'
Is this the best approach? or
Is there any alternative to this process of data insertion and selection for these particular type of data (contains quote)? Any suggestion ?
No you don't have to double the escaped quotes:
select *
from employees
where name = 'john''s'

Column isn't being found?

I am trying to display the username, last name, join date and country name for all the users who joined after the 13th January 2017 in ascending order. However each time I try to call any column, I get the error message "Column: Invalid Identifier".
SELECT Username, LastName, JoinDate, CountryName
FROM BR_USER, BR_COUNTRY
WHERE JoinDate = '01-JAN-17' AND JoinDate IS NOT NULL
ORDER BY JoinDate ASC;
Here is an image of how the BR_USER table is created.
Simple codes like:
SELECT UserId
FROM BR_USER;
Gives the same invalid identifier error, help
As the documentation explains:
Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.
If you define a column name with double quotes, you are condemned to using the double quotes whenever your reference that column. Or table or anything else with a name.
Actually, I don't think the documentation is 100% correct. Oracle uppercases all identifiers for resolution. So, if you define a quoted identifier with all upper case, then it will work without quotes. So this works:
create table t (
"COL" int
);
select "COL", COL, col
from t;
Here is a db<>fiddle.
But who wants to remember such rules -- rules so arcane and complex that the documentation is even misleading.
Simple solution: Don't use double quotes.

Oracle - Invalid Identifier for column name

I ran into an interesting situation with a NUMBER field.
Here is a simplified example.
When I select a particular field have happens to be 29 characters long, it works just fine.
select FIELD_NAME_THAT_IS_29_CHAR_XX
from table;
In the report where this query is being used, the query does not return headers (this is an eText type XML publisher report). But when I run the query with a UNION, selecting the header names, I get an invalid identifier error.
SELECT "FIELD_NAME_THAT_IS_29_CHAR_XX" FROM dual
UNION
SELECT FIELD_NAME_THAT_IS_29_CHAR_XX FROM table1;
Returns:
ORA-00904: "LINE_RECEIPT_AMNT_AT_COST_USD": invalid identifier
The max length of a field name in Oracle DB is 30 chars. Am I hitting this limitation? I think not as this, for example:
SELECT "FIELD_NAME_THAT_IS_29_CHAR_XXxxxxx" FROM dual;
..gives:
ORA-00972: identifier is too long
What is wrong with the UNION?
Single quotes do not work either. This is a NUMBER field.
SELECT 'FIELD_NAME_THAT_IS_29_CHAR_XX' FROM dual
UNION
SELECT FIELD_NAME_THAT_IS_29_CHAR_XX FROM table1;
..gives:
ORA-01790: expression must have same datatype as corresponding expression
The problem is with the double quotes
Oracle SQL allows us to ignore the case of database object names provided we either create them with names all in upper case, or without using double quotes. If we use mixed case or lower case in the script and wrapped the identifiers in double quotes we are condemned to using double quotes and the precise case whenever we refer to the object or its attributes:
Instead use single quotes which essentially works and will give you the result you want
http://sqlfiddle.com/#!4/dce84/4
#Standin answer lead me to solution.
Because the selected field is a number, just need to cast is as a char - along with the single quotes.
SELECT 'FIELD_NAME_THAT_IS_29_CHAR_XX' FROM dual
UNION
SELECT to_char(FIELD_NAME_THAT_IS_29_CHAR_XX) FROM table1;

Oracle DB quote column names

When using regular tables, its fine to use the following Oracle SQL query:
SELECT max(some_primary_key) FROM MyTable
However, when using Database Objects (i.e. a table of an object), this yields to the following error:
ORA-00904: "SOME_PRIMARY_KEY": invalid identifier
When quoting the column name, like this:
SELECT max("some_primary_key") FROM MyTable
This works like expected. Why is it necessary to escape column names when working with Objects, but not with Tables?
It doesn't have to do with objects or tables, it has to do with how these objects/tables have been created.
If you do
create table "blabla" then you always need to address this table with "blabla", if you do create table blabla then you can address this table via BLABLA or blabla or bLabLa. Using " " makes the name case sensitive and that is the reason why most developers don't use " " because usually you don't want case sensitive names .
Database Object Naming Rules
Every database object has a name. In a SQL statement, you represent
the name of an object with a quoted identifier or a nonquoted
identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you
must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
You can use either quoted or nonquoted identifiers to name any
database object. However, database names, global database names, and
database link names are always case insensitive and are stored as
uppercase. If you specify such names as quoted identifiers, then the
quotation marks are silently ignored. Refer to CREATE USER for
additional rules for naming users and passwords.
To summarize this
When you do :
SELECT max(some_primary_key) FROM MyTable
Oracle assume that your column was declared like this :
CREATE TABLE MyTable (
some_primary_key INT,
...
)
Seing the resulting error, it's not the case. You obviously declared it like this :
CREATE TABLE MyTable (
"some_primary_key" INT,
...
)
And you should thus always refer to that column using double quotes and proper case, thus :
SELECT max("some_primary_key") FROM MyTable
The bible : Oracle Database Object Names and Qualifiers
[TL;DR] The simplest thing to do is to never use double quotes around object names and just let oracle manage the case-sensitivity in its default manner.
Oracle databases are, by default, case sensitive; however, they will also, by default, convert everything to upper-case so that the case sensitivity is abstracted from you, the user.
CREATE TABLE Test ( column_name NUMBER );
Then:
SELECT COUNT(column_name) FROM test;
SELECT COUNT(Column_Name) FROM Test;
SELECT COUNT(COLUMN_NAME) FROM TEST;
SELECT COUNT(CoLuMn_NaMe) FROM tEsT;
SELECT COUNT("COLUMN_NAME") FROM "TEST";
Will all give the same output and:
DESCRIBE test;
Outputs:
Name Null Type
----------- ---- ------
COLUMN_NAME NUMBER
(Note: Oracle's default behaviour is to convert the name to upper case.)
If you use double quotes then oracle will respect your use of case in the object's name (and you are then required to always use the same case):
CREATE TABLE "tEsT" ( "CoLuMn_NaMe" NUMBER );
(Note: Both the table and column name are surrounded in double quotes and now require you to use exactly the same case, and quotes, when you refer to them.)
Then you can only do (since you need to respect the case sensitivity):
SELECT COUNT("CoLuMn_NaMe") FROM "tEsT";
And
DESCRIBE "tEsT";
Outputs:
Name Null Type
----------- ---- ------
CoLuMn_NaMe NUMBER
(Note: Oracle has respected the case sensitivity.)
I created one object in Oracle 11g:
CREATE OR REPLACE TYPE MyType AS OBJECT (
some_property NUMBER(20),
CONSTRUCTOR FUNCTION MyType(some_property number default 123) RETURN SELF AS RESULT
) NOT FINAL;
/
CREATE OR REPLACE TYPE BODY MyType AS
CONSTRUCTOR FUNCTION MyType(some_property number default 123)
RETURN SELF AS RESULT
AS
BEGIN
SELF.some_property := some_property;
RETURN;
END;
END;
/
---Created a table of my object
CREATE TABLE MYTABLE OF MYTYPE ;
---issued the statement.
SELECT max(some_property) FROM MYTABLE;
Its working fine for me without quotes. Not sure why its not working in your case. Whats your oracle version ?