20

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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • Are you sure that FOO is a CLOB and not a BLOB? You'd get an incompatible type error comparing a CLOB to a BLOB-- you'd need to compare a BLOB column to empty_blob(). – Justin Cave Feb 06 '09 at 18:38
  • FOO is definitely a CLOB, and the default value is empty_clob() – Hank Gay Feb 06 '09 at 19:47

5 Answers5

21

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
--------------------------------------------------------------------------------
Steve K
  • 19,408
  • 6
  • 52
  • 50
  • This is the current workaround, but it seems overly complicated for such a simple task. – Hank Gay Feb 06 '09 at 19:48
  • 1
    In case you want to include NULL values you may use `select * from bar where nvl(dbms_lob.getlength(foo),0) = 0;` – FuePi Jul 23 '18 at 14:15
18

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
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
3

something like this should work for initialization:

DECLARE
   dummy  clob;
   dummy2 clob;
BEGIN
     dummy := empty_clob();
      IF dummy = empty_clob() THEN
         dummy2 := dummy;
      END IF;
END;
Igor Zelaya
  • 4,167
  • 4
  • 35
  • 52
0

A simple way to test for empty clobs in SQLplus is to convert all the CLOBS to varchar2 (using the TO_CHAR function) before performing the test:

SELECT *
  FROM table1
  WHERE TO_CHAR(table1.column1) IS NULL
m00am
  • 5,910
  • 11
  • 53
  • 69
0
DECLARE
    dummy    CLOB := 'fxsgf';
    dummy1   CLOB;
BEGIN
    IF dummy1 = EMPTY_CLOB ()
    THEN
        DBMS_OUTPUT.put_line ('Dummy1 is empty');
    ELSE
        DBMS_OUTPUT.put_line ('Dummy1 is not empty');
    END IF;

    IF dummy = EMPTY_CLOB ()
    THEN
        DBMS_OUTPUT.put_line ('Dummy is empty');
    ELSE
        DBMS_OUTPUT.put_line ('Dummy is not empty');
    END IF;
END;
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39