Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PL/SQL Solution for Converting Long to VarChar2

512504May 22 2006 — edited May 22 2006
I recently had to convert a bunch of text stored in Long fields to a new VarChar2(2000) field in the same table.

As you know if you've ever tried, you can't use a basic SQL the update statement will not do this, e.g. update table1 set field2 = field1 (where field2 is VarChar(2000) and field1 is Long) fails as does update table1 set field2 = substr(field1, 1, 2000).

Interestingly PL/SQL's implementation of SubStr does work with Long fields. I was able to put together the following anonymous PL./SQL block to accomplish the task (msgtext is varchar(2000), msgtext2 is Long):

DECLARE
CURSOR message_cur
IS
SELECT ROWID, msgtext, msgtext2
FROM tlrcmmessages
FOR UPDATE;
message_rec message_cur%ROWTYPE;
BEGIN

OPEN message_cur;

LOOP
FETCH message_cur
INTO message_rec;

EXIT WHEN message_cur%NOTFOUND;

UPDATE tlrcmmessages
SET msgtext = SUBSTR(message_rec.msgtext2, 1, 2000)
WHERE CURRENT OF message_cur;

END LOOP;

CLOSE message_cur;
END;

I hopes this helps others out there in similar circumstances.

Good luck,
Rob Bratton, Bratton Enterprises
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 19 2006
Added on May 22 2006
1 comment
333 views