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

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.

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

ways to check for invalid characters? oracle sql

Looking for ways to filter out special signs, letters etc. from studentID in oracle SQL and show those records as invalid.
What is the best way to filter out letters a-Z and other characters? (only leaving numbers)
SELECT replace(Translate(studentid,'a-Z<>!-\+=/&', '??'),'?','') as StudentID, 'Invalid Characters in Student ID'
FROM students
The simplest approach is to use regular expressions. For example
select studentid
from student
where regexp_like(studentid, '\D')
;
\D means non-digit character; if the studentid contains at least one such character, in any position, then it will appear in the output. Note that null will not be flagged out, assuming it may appear in the column; perhaps the column is primary key in which case it can't be null. But this would apply to other tables as well, where studentid may be null.
If you have a very large table, or if you must perform this check often, you may want a less simple, but better performing query. Then you would want to use standard string functions, like you were trying to. Something like this will work:
select studentid
from student
where translate(studentid, 'x0123456789', 'x') is not null
;
translate will translate x to itself, and all digits to null (that is, all digits will be removed). The x trick is needed because the last argument must not be null. If the translation doesn't remove all characters from the string, then the studentid will appear in the output, as required.
If you need to show exactly which characters are non-digits (although that should be obvious), you can add the result of translate to the select clause. Note though that if a student id has, for example, trailing spaces, that will not be evident either from looking at the student id or at the result of translate. You may want to add something like dump(studentid) to select; if you are not familiar with dump, you may want to read a bit about it - it is extremely useful in diagnosing such problems, and easy to learn.
Once you find and handle all the exceptions, you may want to add a constraint to the column, to require all student id's to consist entirely of digits. Then you won't have to put up with this kind of errors anymore.
If you want to allow numbers only, column datatype should have been NUMBER, not VARCHAR2.
[EDIT] That's wrong, though - see #mathguy's comment about it, saying that there are situations where values do consist of digits only, but - due to leading zeros - you can't use the NUMBER datatype.
A simple option is to use regexp_like and return rows that contain anything but digits:
SQL> with students (studentid) as
2 (select '12345' from dual union all
3 select 'ABC12' from dual union all
4 select '23x#2' from dual
5 )
6 select studentid
7 from students
8 where not regexp_like(studentid, '^\d+$');
STUDE
-----
ABC12
23x#2
SQL>
You could also use below solution taking advantage of translate function.
select studentid
from students
WHERE translate(studentid, '`0123456789', '`') IS NOT NULL
;
demo

How to use keyword in Postgres namespace? [duplicate]

This question already has answers here:
Escaping keyword-like column names in Postgres
(3 answers)
Not able to create the specific column in Postgres
(1 answer)
Postgresql: literal table names
(1 answer)
Closed 1 year ago.
I have a Postgres db. The tables are each in separate namespaces, which are named using two character state codes. The problem is this: for the state Indiana, the state code is "in". I am trying to execute this query:
SELECT city_name
FROM in.places
But I keep getting an error complaining about the 'in', presumably because there is a reserved keyword IN. How can I query the tables inside the 'in' namespace?

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'

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.