Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing NULL keyword error when trying to avoid duplicates [closed]

Tags:

sql

oracle11g

I'm trying to do an update to a table that contains records pertaining to people under Oracle 11g. I have a column that contains IDs, some of which contain hyphens. The table has a constraint on this ID so two different people can't have the same ID. My goal is to remove the hyphens, while avoiding any constraint violations when I do an update. So I tried this query to detect records that would violate the constraint:

SELECT count(*) FROM people
WHERE externalid LIKE '%-%' AND
replace(externalid, '-') IS IN (SELECT externalid FROM people);

This then fails with an ORA-00908: missing NULL keyword. I know this is due to that last line, but I don't know how else to structure that part of the query to detect the records that have the non-hyphenated ID already in use. How can I detect these records that would violate the constraint?

like image 340
Keen Avatar asked Feb 28 '13 19:02

Keen


People also ask

What is missing NULL keyword in SQL?

The NULL keyword is valid in most contexts where you can specify a value. What it specifies, however, is the absence of any value (or an unknown or missing value). Within SQL, the keyword NULL is the only syntactic mechanism for accessing a NULL value. NULL is not equivalent to zero, nor to any specific value.

How do you modify your SQL so that a duplicate value is not created?

Solution 1: Modify your SQL You can modify your SQL to ensure you're not inserting a duplicate value. If you're using ID values for a primary key, it's a good idea to use a sequence to generate these values. This way they are always unique. You can use the sequence.

How do I fix an invalid identifier in Oracle?

Ora-00904 Error Message “Invalid Identifier” This error is most common when querying a SELECT statement. To resolve this error, first check to make sure the column name being referenced exists. If it does not exist, you must create one before attempting to execute an SQL statement with the column.


1 Answers

The IS keyword is used as in something IS NULL, not with the IN keyword. That's why the database thinks that there is a NULL missing. Just remove the IS:

SELECT count(*) FROM people
WHERE externalid LIKE '%-%' AND
replace(externalid, '-') IN (SELECT externalid FROM people);
like image 61
Guffa Avatar answered Nov 15 '22 06:11

Guffa