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?
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With