Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL Select where clob field is not empty/null

I am trying to get rows from an archive database for populating a test environment DB. I need rows where a specific field called "AUDIT_LOG" which is CLOB cannot be empty null.

I have tried the following simple select statements but I get

ORA-00932: inconsistent datatypes: expected - got CLOB

The statements I have tried:

SELECT * FROM SIEBEL.S_AUDIT_ITEM WHERE AUDIT_LOG = ''

SELECT * FROM SIEBEL.S_AUDIT_ITEM WHERE AUDIT_LOG != NULL

SELECT * FROM SIEBEL.S_AUDIT_ITEM WHERE AUDIT_LOG = 0

Does a NULL in CLOB equate to an empty character string. I have a feeling maybe length can be used for the column.

Thanks

like image 290
tomaytotomato Avatar asked Jan 09 '13 15:01

tomaytotomato


People also ask

How check CLOB is null in SQL?

You can use a regular expression for that. Try this: declare Data1 Clob; begin Data1 := 'ab c d'; -- replace all the whitespace in Data1 with null if dbms_lob. getlength(regexp_replace(Data1,'[[:space:]]'))=0 then dbms_output.

Can a CLOB be null?

an empty_clob() is a value - it is not null, it is just "empty", no data in it yet. The empty_clob() is not NULL, it is an empty clob of length zero.

How do you know if CLOB is empty?

Similarly, to check for empty CLOB , change the variable type to clob and replace the empty_blob() function with empty_clob() function in the above PL/SQL code.

IS NOT NULL in Oracle where clause?

Here is an example of how to use the Oracle IS NOT NULL condition in a SELECT statement: SELECT * FROM customers WHERE customer_name IS NOT NULL; This Oracle IS NOT NULL example will return all records from the customers table where the customer_name does not contain a null value.


2 Answers

to check NULL, regardless of datatype, you use IS NOT NULL or IS NULL

WHERE AUDIT_LOG IS NOT NULL

But keep in mind that for CLOBs, an EMPTY_CLOB() has no characters but is not the same as NULL. If you want to exclude EMPTY_CLOB(), use @user3837669's answer that uses a LENGTH comparison.

like image 82
DazzaL Avatar answered Oct 12 '22 18:10

DazzaL


SELECT * FROM SIEBEL.S_AUDIT_ITEM WHERE length(AUDIT_LOG) > 0
like image 40
user3837669 Avatar answered Oct 12 '22 18:10

user3837669