Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3824
  • Last Modified:

Interesting Oracle question DECODE vs CASE with CLOB conversion

I noticed something when answering a question today. Assume a table with a CLOB field.

create table t(text clob);

-- insert some data

Both of these queries throw ORA-00932

SQL> select case when 1=1 then '' else text end from t;
SQL> select case when 1=1 then cast('' as clob) else text end from t;

ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB

But these all work. Note the decode() does not use any casting or conversion.

SQL> select case when 1=1 then to_clob('') else text end from t;
SQL> select case when 1=1 then null else text end from t;
SQL> select decode(1, 1, '', text)  from t;

In one case, the '' is either being implicitly cast to CLOB or the whole expression is being cast to VARCHAR.

Anyone have some insight?
3 Solutions
mrjoltcolaAuthor Commented:
I can understand perhaps why the others work, null is typeless, and to_clob('') is an explicit conversion.

What I don't get is why cast('' as clob) does not work and why decode() works without any casting or conversion at all.
mrjoltcolaAuthor Commented:
Aha, I added some clobs of 6000 bytes in length and some things change.

select length(case when 1=0 then null else text end) from t;    <-- works fine, returns 6000

However, the original DECODE() that worked fails. It appears that DECODE() is implicitly converting to VARCHAR based on the type of the 1st expression in the list.

SQL> select length(decode(0, 1, '', text))  from t;
select length(decode(0, 1, '', text))  from t
ERROR at line 1:
ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual:
6528, maximum: 4000)

SQL> select length(decode(0, 1, to_clob(''), text))  from t;


I think I have answered the question, DECODE() converts to the 1st expression.
very interesting indeed . the only explanations for difference in implicit and explicit clob conversions that implicit works after execution plan and by then expression is wrong.Decode seem to have clob overlay that case doesn't
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

mrjoltcolaAuthor Commented:
Well I thought it was some sort of "clob overlay" but I figured it out, and it is not. DECODE() is casting to VARCHAR() based on the type of the 1st expression ''

  DECODE(0, 1, 'Y', '', TEXT)

But in that case an ORA-22835 will result if there are any > 4000 length. By random luck, if the author writes the CLOB type first in the expression:

  DECODE(0, 1, TEXT, '')  <-- this works due to the left-to-right order, Oracle chooses CLOB to convert to

The danger here is that the author may test the code and think it works fine if all his clobs are small clobs. I actually have an app where most clobs are 2-3k and the 4k+ clob is an exception to the rule. So the lesson here is if you are testing code with clobs, make sure the data is not getting truncated/converted to a varchar without your knowledge.

So I think it would be safest to explicitly provide the conversion so the ordering is not left up to Oracle.

  DECODE(0, 1, TO_CLOB(''), TEXT)

So there is no ordering dependency and it is not left up to Oracle

Thanks, I enjoy thinking out loud and teaching myself things... :). I will leave this open to see if there are any other interesting comments.

per 24790367

"null is typeless"

that's not necessarily true.

The keyword NULL has no type.

''  is a typed NULL of varchar2 type.

with decode and clob,  as noted in 24790528, the type of the function is based on the type of the first expression returned
mrjoltcolaAuthor Commented:
>>>>"null is typeless"

>>that's not necessarily true.

>>The keyword NULL has no type.

Yes, the keyword NULL is typeless. Not sure what you are saying here. "Has no type" and "typeless" are equivalent to me. When used in an expression, NULL has no type meaning unless gathered by surrounding context.

But, my only remaining question is regarding the CASE statement and CAST from '' to CLOB, it appears casting with CAST keyword from '' to CLOB is not allowed or just doesn't work?

From the Oracle documentation -
"CAST does not support LONG, LONG RAW, any of the LOB datatypes, or the Oracle-supplied types."
mrjoltcolaAuthor Commented:
I should have looked that up myself. Thanks!
sorry to chime in late...

when I said "typeless" and "has no type" I meant them to be equivalent.

What I was trying to clarify was your statement "null is typeless",  a null value is not necessarily typeless.  

 the word "NULL" by itself is typeless

but '' is null but is has a varchar2 type.

similarly  to_number(NULL)  is a null number

that's all, nothing super insightful.  :)

mrjoltcolaAuthor Commented:
Okey doke. I suppose I agree with you now.. :)

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now