Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if a column equals empty_clob() in Oracle?

Tags:

The naïve FOO = empty_clob() complains about incompatible types. I tried Googling, but (once again) had little success searching for help with Oracle. Thanks.

like image 741
Hank Gay Avatar asked Feb 06 '09 18:02

Hank Gay


People also ask

How do you check if a column is null in CLOB?

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. put_line('First try: Data1 is null'); else dbms_output.

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.

How do you write greater than or equal to in Oracle?

In Oracle, you can use the >= operator to test for an expression greater than or equal to. SELECT * FROM suppliers WHERE supplier_id >= 1000; In this example, the SELECT statement would return all rows from the suppliers table where the supplier_id is greater than or equal to 1000.


2 Answers

Are you just wanting to check for a CLOB that doesn't have any length? While not exactly what your asking, it's basically the same thing?

select *
  from bar
 where dbms_lob.getlength(foo) = 0;

Here is the complete test:

SQL> create table bar (foo clob);

Table created.

SQL> insert into bar values (empty_clob());

1 row created.

SQL> select *
  2    from bar
  3  where dbms_lob.getlength(foo) = 0;

FOO
--------------------------------------------------------------------------------
like image 142
Steve K Avatar answered Oct 24 '22 10:10

Steve K


If you are trying to do the comparison in PL/SQL, you can just test equality as Igor's solution does

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2     dummy  clob;
  3  BEGIN
  4       dummy := empty_clob();
  5        IF dummy = empty_clob() THEN
  6           dbms_output.put_line( 'Dummy is empty' );
  7        ELSE
  8           dbms_output.put_line( 'Dummy is not empty' );
  9        END IF;
 10* END;
SQL> /
Dummy is empty

PL/SQL procedure successfully completed.

If you are trying to do this in SQL, thougyh, you need to use the DBMS_LOB.COMPARE function. A LOB column in a table is really a LOB locator (i.e. pointer), so what you really care about is that the value pointed to by the LOB is comparable to the value pointed to by the LOB locator returned by the EMPTY_CLOB() function.

SQL> desc bar
 Name                                      Null?    Type
 ----------------------------------------- -------- ------------------------

 FOO                                                CLOB

SQL> insert into bar values ('123');

1 row created.

SQL> insert into bar values( empty_clob() );

1 row created.

SQL> insert into bar values( empty_clob() );

1 row created.

SQL> ed
Wrote file afiedt.buf

  1  select count(*)
  2    from bar
  3*  where dbms_lob.compare( foo, empty_clob() ) = 0
SQL> /

  COUNT(*)
----------
         2

SQL> ed
Wrote file afiedt.buf

  1  select count(*)
  2    from bar
  3*  where dbms_lob.compare( foo, empty_clob() ) != 0
SQL> /

  COUNT(*)
----------
         1
like image 41
Justin Cave Avatar answered Oct 24 '22 10:10

Justin Cave