Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Interesting Oracle question DECODE vs CASE with CLOB conversion

Posted on 2009-07-06
10
Medium Priority
?
3,681 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 800 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 800 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 1200 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

719 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