Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert CLOB to VARCHAR2 inside oracle pl/sql

Tags:

oracle

plsql

clob

I have a clob variable, need to assign it to varchar2 variable. The data inside clob var is less than 4000 (i..e varchar2's maxsize) oracle10+

I tried

  report_len  := length(report_clob);
  report      := TO_CHAR(dbms_lob.substr(report_clob, report_len, 1 ));
  report_clob := null;

but it turns report into long value which I see while debugging. Also when I call this sql (proc) from my C# code. It complains saying buffer too small, because I send parameter as per varchar, but the above conversion might be turning it into long value.

I even tried direct assignment

   report_clob := report 

getting the same result.

EDIT

Ok, to answer the questions below please see: I debug using test script in PL/SQL developer. report variable is varchar2(4000). When I step after 2nd line. report shows to be a long value and it just says (Long Value) . cant even see the contents.

report and report_clob are out variable from the procedure. This procedure is called from C# code.

There is an exception string buffer too small in C# when I call this procedure. I have given 5000 as size of report variable in C# sufficient to receive 4000 max characters value from the procedure. So I guess problem doesn't lie there.

And when I assign report:= 'some string....' then C# call works fine.

So my investigation says that report := transform (report_clob) is making report become long value or some such thing (weird) which makes C# code problematic to handle larger value in 5000 varchar out parameter.

Any more detail I will be happy to provide.

like image 879
Munish Goyal Avatar asked Oct 12 '12 13:10

Munish Goyal


People also ask

Can we convert CLOB to VARCHAR2 in Oracle?

CLOB is an "unlimited text" variable. In order to convert to varchar first you must fix the maximum varchar size (4000) and then convert to char. Sorry! There is not using CLOB data-type for source column LMN from source table PQR.

Can we convert CLOB to string?

To convert CLOB data type to string Reader r = clob. getCharacterStream(); Read each character one by one from the retrieved Stream of characters and append them to the StringBuilder or StringBuffer.

How do you query CLOB data in Oracle SQL Developer?

select dbms_lob. substr(clob_filed, 1000000, 1) from exportlog; I run this sql in sql developer and sqlplus in command prompt. both always return NULL.

How do I convert a CLOB to a VARCHAR2?

In PL/SQL a CLOB can be converted to a VARCHAR2 with a simple assignment, SUBSTR, and other methods. A simple assignment will only work if the CLOB is less then or equal to the size of the VARCHAR2. The limit is 32767 in PL/SQL and 4000 in SQL (although 12c allows 32767 in SQL). For example, this code converts a small CLOB through ...

How can I convert from long to VARCHAR2?

How can i convert from long to varchar2? I want do that in single sql select. The simplest method is to use a cursor in PL/SQL. See this Oracle-developer.net article The second method simply takes advantage of PL/SQL's ability to convert LONG data to VARCHAR2 while fetching from cursors.

What is the maximum size of a CLOB in PL SQL?

The limit is 32767 in PL/SQL and 4000 in SQL (although 12c allows 32767 in SQL). For example, this code converts a small CLOB through a simple assignment and then coverts the beginning of a larger CLOB. LONG? The above code does not convert the value to a LONG.

What is the size of a VARCHAR2 in SQL Server?

o varchar2 will be limited to 4000 bytes in SQL - 32000 - not going to happen. o dbms_lob.substr ( CLOB ) returns a varchar2 - I'm assuming MSG_TEXT is a CLOB here (you don't say). Now cast_to_varchar2 is expecting a RAW type (binary).


2 Answers

Quote (read here)-

When you use CAST to convert a CLOB value into a character datatype or a BLOB value into the RAW datatype, the database implicitly converts the LOB value to character or raw data and then explicitly casts the resulting value into the target datatype.

So, something like this should work-

report := CAST(report_clob AS VARCHAR2(100));

Or better yet use it as CAST(report_clob AS VARCHAR2(100)) where ever you are trying to use the BLOB as VARCHAR.

like image 200
AnBisw Avatar answered Sep 21 '22 06:09

AnBisw


Converting VARCHAR2 to CLOB

In PL/SQL a CLOB can be converted to a VARCHAR2 with a simple assignment, SUBSTR, and other methods. A simple assignment will only work if the CLOB is less then or equal to the size of the VARCHAR2. The limit is 32767 in PL/SQL and 4000 in SQL (although 12c allows 32767 in SQL).

For example, this code converts a small CLOB through a simple assignment and then coverts the beginning of a larger CLOB.

declare
    v_small_clob clob := lpad('0', 1000, '0');
    v_large_clob clob := lpad('0', 32767, '0') || lpad('0', 32767, '0');
    v_varchar2   varchar2(32767);
begin
    v_varchar2 := v_small_clob;
    v_varchar2 := substr(v_large_clob, 1, 32767);
end;

LONG?

The above code does not convert the value to a LONG. It merely looks that way because of limitations with PL/SQL debuggers and strings over 999 characters long.

For example, in PL/SQL Developer, open a Test window and add and debug the above code. Right-click on v_varchar2 and select "Add variable to Watches". Step through the code and the value will be set to "(Long Value)". There is a ... next to the text but it does not display the contents. PLSQL Developer Long Value

C#?

I suspect the real problem here is with C# but I don't know how enough about C# to debug the problem.

like image 20
Jon Heller Avatar answered Sep 18 '22 06:09

Jon Heller