Skip to content

Instantly share code, notes, and snippets.

@uraimo
Last active March 28, 2022 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uraimo/8928bb5a3e7ff656e56f2a57ef4509cd to your computer and use it in GitHub Desktop.
Save uraimo/8928bb5a3e7ff656e56f2a57ef4509cd to your computer and use it in GitHub Desktop.
Oracle string in LONG field to VARCHAR2 without temporary tables or anything else, pure PL/SQL

LONG fields were used in Oracle DB during the Dark Ages to store long text, but they are not selectable and cannot be used in where clauses with LIKE. A multitude of broken solutions exist (to_lob,substr,weird macros, converting the value into new tables, etc...) online to be able to search into this field with a normal query with LIKE, but they are mostly broken. This sample declares a temporary function for the conversion (size up to 4k, it's an oracle limit, some sei you can go up to 32k in PL/SQL but this does not seem the case) and then uses it in a query, ugly but simple.

Errors you could see while handling LONG fields:

ORA-00997: illegal use of LONG datatype
ORA-06502: PL/SQL: numeric or value error

How to do a simple select from where LONG_FIELD like '%something%' in pure PL/SQL in one go:

with function convertLong(r rowid)
    return varchar2 is
        tmp varchar2(4000)
    begin
        select LONG_FIELD into tmp from TABLE_WITH_LONG_FIELD where rowid=r;
        return tmp;
    exception
    when VALUE_ERROR then
        return '';
    end;
select t.ID, t.DESC, t.LONG_FIELD
from TABLE_WITH_LONG_FIELD t
where convertLong(t.rowid) like '%find this text%';

Enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment