Solved

Interesting Oracle question DECODE vs CASE with CLOB conversion

Posted on 2009-07-06
10
3,402 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:mrjoltcola
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 40

Author Comment

by:mrjoltcola
ID: 24790367
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.
0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 200 total points
ID: 24790446
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;

6000



I think I have answered the question, DECODE() converts to the 1st expression.
0
 
LVL 21

Expert Comment

by:oleggold
ID: 24790487
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 200 total points
ID: 24790528
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.

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24791635
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
0
 
LVL 40

Author Comment

by:mrjoltcola
ID: 24793825
>>>>"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?

0
 
LVL 32

Accepted Solution

by:
awking00 earned 300 total points
ID: 24794451
From the Oracle documentation -
"CAST does not support LONG, LONG RAW, any of the LOB datatypes, or the Oracle-supplied types."
0
 
LVL 40

Author Closing Comment

by:mrjoltcola
ID: 31600420
I should have looked that up myself. Thanks!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24796132
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.  :)



0
 
LVL 40

Author Comment

by:mrjoltcola
ID: 24796153
Okey doke. I suppose I agree with you now.. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question