You get this error in Oracle when you are missing a comparison operation, such as =
— as John Maillet already noted.
My concern is the second part of the where
clause:
where to_date(substr(COLUMN_NAME, 1, 14), 'YYYYMMDDHH24MISS') >=
to_date('MIN_DATE', 'YYYYMMDDHH24MISS')
You have MIN_DATE
in single quotes. This is interpreted as a string with eight letters in it, starting with 'M'
and ending with 'E'
. This is not interpreted as a variable. Presumably you mean:
where to_date(substr(COLUMN_NAME, 1, 14), 'YYYYMMDDHH24MISS') >=
to_date(MIN_DATE, 'YYYYMMDDHH24MISS')
You should only use single quotes for string and date constants.
I should add that you should be able to do this comparison without having to convert to dates:
where left(COLUMN_NAME, 14) = MIN_DATE
ORA-00920: invalid relational operator error occurs when a SQL query’s WHERE clause contains an invalid relational operator. Below is a list of the relational operators. Oracle could not find the relationship between two operands in the sql query if the WHERE clause uses a relational operation other than those listed. In this case, the error ORA-00920: invalid relational operator will be displayed in the oracle. Oracle does not recognize the invalid relational operator, therefore the WHERE clause of the sql query could not be executed. In this situation, the sql query will fail to execute.
The relational operators compare two values and return a boolean value, TRUE or FALSE. When the two operands are compared using the relational operator, the result is TRUE or FALSE. Relationship operators that are not supported by Oracle are considered invalid. Oracle will throw an error ORA-00920: invalid relational operato if any invalid relational operators are used in a sql query.
The relational operators supported in oracle are =, !=, ^=, <>,<, <=, >, >=, ALL, ANY, IN, NOT IN, BETWEEN, NOT BETWEEN, LIKE, NOT LIKE, EXISTS, NOT EXISTS, IS NULL, IS NOT NULL .
When the ORA-00920 error occurs
If you use an invalid relational operator between two operands in the SQL query’s WHERE clause, Oracle will be unable to determine the relationship between the two operands. The sql query’s WHERE clause was unable to complete the request. The sql query will not be executed. The Oracle error ORA-00920: invalid relational operato will be thrown in this scenario.
Program
select * from employee where id=>1;
Error
ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
*Cause:
*Action:
Root Cause
The relational operators compare two values and return a boolean value of TRUE OR FALSE. In programming, relational operators are used to make decisions. The relation could not be found if the relational operations used in the Oracle SQL query were incorrect. The sql query was unable to be executed. Oracle-supported relational operators should be used in the WHERE clause of a sql query
Solution 1
If the relational operator used in the WHERE clause is not in the list of those supported by Oracle, replace it with one that is. The relational operator might be misplaced or incorrectly written. To correct the error, change the relational operator in the WHERE clause. The relational operator should be one of the ones listed above.
Program
select * from employee where id=>1;
ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
Solution
select * from employee where id >= 1;
Solution 2
Oracle could not identify the relational operator if the closing parenthesis was missed in the WHERE clause. The invalid relational operator error message will be shown. Oracle could not identify the relational operator in the WHERE clause if the open and close parentheses were misplaced or inserted extra in the sql query. If there is an issue with the open and close parenthesis, return to the query and rectify the problem. This will resolve the error.
Program
select * from employee where ltrim(name))='yawin';
ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
Solution
select * from employee where ltrim(name) ='yawin';
Solution 3
The NOT operator should be placed in the correct order in the WHERE clause, if the NOT operator is misplaced in the sql query, the invalid relational operator error will be displayed. The NOT operator will be as follows, NOT IN, NOT LIKE, NOT BETWEEN, NOT EXISTS, IS NOT NULL.
Program
select * from employee where id not is null;
ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
Solution
select * from employee where id is not null;
I don’t quite trust what you’re posted…
Your query should throw ORA-00979: Not a GROUP BY expression
. This is because the columns not included in an analytic function, namely username
are not reflected in your group by
.
ORA-00920 implies that you are missing one of the following: =, <>, not in, in, !=, is not null, is null, not like, like
etc. Are you sure you’ve posted the correct query?
In 11.2, creating something that approximates your table, like so:
create table host
( username varchar2(100)
, created_dt date );
insert into host
select level, sysdate - level
from dual
connect by level <= 10
;
insert into host
select chr(ascii(level) + 32), sysdate - level
from dual
connect by level <= 10
;
commit ;
and then running the query posted results in ORA-00979. Changing this to the following work’s fine.:
select case when regexp_like(username, '^d+$') then 'GRP_OTHERS'
else username end as username
, count(*)
from host
where created_dt between
to_date('2012-may-23 00:00:00', 'yyyy-mon-dd hh24:mi:ss') and
to_date('2012-may-23 23:59:59', 'yyyy-mon-dd hh24:mi:ss')
group by case when regexp_like(username, '^d+$') then 'GRP_OTHERS'
else username end
;
This can alternatively be re-written as:
select distinct username
, count(*) over ( partition by case when regexp_like(username, '^d+$')
then 'GRP_OTHERS'
else username end )
from host
where created_dt between
to_date('2012-may-23 00:00:00', 'yyyy-mon-dd hh24:mi:ss') and
to_date('2012-may-23 23:59:59', 'yyyy-mon-dd hh24:mi:ss')
I think this second query is more like what you want. It returns the actual user-name, but groups all those that are just digits together. Just replace the username column returned by the case if you want to see GRP_OTHERS
instead.
It’s slightly better not to use the mon
format model in Oracle as it’s not, necessarily, consistent across languages. Use mm
instead.
As you’re using 9i you can use translate instead. Replace the regexes with:
trim(translate(username,'0123456789',' ')) is null
This replaces numbers with nothing and then checks to see if there’s anything left…
ORA-00920
ORA-00920: неправильный реляционный оператор
Причина:
Условие поиска было введено с неправильным или пропущенным реляционным оператором.
Действие:
Включите правильный реляционный оператор такой как =, !=, ^=, <>, >, <, >=, <=, ALL, ANY, NOT BETWEEN, NOT EXISTS, NOT IN, IS NOT NULL, или NOT LIKE.
In this guide, we’ll dive into the ORA-00920 error, understand the reasons behind it, and learn how to fix it. As a developer, you may encounter this error while working with Oracle databases. By following the steps in this guide, you’ll be able to quickly identify and resolve the issue.
Table of Contents
- Introduction to ORA-00920 Error
- Common Causes of the ORA-00920 Error
- How to Fix the ORA-00920 Error
- Step 1: Identify the Invalid Relational Operator
- Step 2: Replace or Remove the Invalid Relational Operator
- Step 3: Test Your Query
- FAQs
Introduction to ORA-00920 Error
The ORA-00920 error occurs when you use an invalid relational operator in a SQL statement. The error message you’ll see is:
ORA-00920: invalid relational operator
Relational operators are used in SQL to compare values and return results based on the comparison. Common relational operators include =
, <>
, >
, <
, >=
, <=
, BETWEEN
, LIKE
, IN
, and IS NULL
. If an invalid relational operator is used or if the syntax is incorrect, you’ll encounter the ORA-00920 error.
Common Causes of the ORA-00920 Error
The primary cause of the ORA-00920 error is using an invalid relational operator or incorrect syntax in SQL statements. This error can occur in various situations, including:
- Using an incorrect relational operator, such as
==
instead of=
. - Using a relational operator with an incorrect number of operands.
- Using a relational operator with an incorrect data type.
- Missing a relational operator between operands in a WHERE or HAVING clause.
How to Fix the ORA-00920 Error
Step 1: Identify the Invalid Relational Operator
The first step in fixing the ORA-00920 error is identifying the invalid relational operator or syntax error in your SQL statement. Carefully review your SQL statement and pay attention to the relational operators used.
Step 2: Replace or Remove the Invalid Relational Operator
Once you have identified the invalid relational operator, replace it with a valid one or remove it if it’s not required.
For example, if you have used ==
instead of =
, update your SQL statement accordingly:
-- Incorrect:
SELECT * FROM employees WHERE salary == 50000;
-- Correct:
SELECT * FROM employees WHERE salary = 50000;
Step 3: Test Your Query
After making the necessary changes, test your SQL statement to ensure that the ORA-00920 error is resolved. If the error persists, double-check your SQL statement for other issues, such as incorrect data types or missing relational operators between operands.
FAQs
1. What are relational operators in SQL?
Relational operators are used in SQL to compare values and return results based on the comparison. Common relational operators include =
, <>
, >
, <
, >=
, <=
, BETWEEN
, LIKE
, IN
, and IS NULL
.
2. Can I use the !=
operator instead of <>
in Oracle?
Yes, the !=
operator is an alternative to the <>
operator in Oracle. Both are used to compare two values for inequality.
3. What is the difference between the WHERE
and HAVING
clause in SQL?
The WHERE
clause is used to filter rows based on specific conditions. It is used with SELECT
, UPDATE
, and DELETE
statements. The HAVING
clause is used to filter the results of a GROUP BY
statement based on a specified condition.
4. How do I use the BETWEEN
operator in SQL?
The BETWEEN
operator is used to filter rows based on a range of values. For example, if you want to retrieve records with a salary between 50,000 and 70,000, you can use the following SQL statement:
SELECT * FROM employees WHERE salary BETWEEN 50000 AND 70000;
5. How do I use the LIKE
operator in SQL?
The LIKE
operator is used to search for a specified pattern in a column. It is often used with the %
wildcard character to represent any sequence of characters. For example, to find employees with first names starting with ‘J’, you can use the following SQL statement:
SELECT * FROM employees WHERE first_name LIKE 'J%';
- Oracle SQL Functions and Operators — Official Oracle documentation for SQL functions and operators
- Oracle SQL Language Reference — Comprehensive guide to Oracle SQL language, including syntax, examples, and reference material