Friday, January 16, 2009

Convert LONG to VARCHAR2

If you have a database table with LONG datatype, and somehow need to convert the data from LONG datatype into VARCHAR2 datatype, here's an example:

CREATE TABLE service_tmp (
service_id NUMBER(22),
activity_id NUMBER(22),
name VARCHAR2(240),
quality VARCHAR2(80),
time_string_in_hole DATE,
time_string_outof_hole DATE
remarks LONG,
remarks_str VARCHAR2(1024)
);

CREATE OR REPLACE PROCEDURE long2varchar IS
CURSOR getrowid IS
SELECT rowid, remarks
FROM service_tmp;
BEGIN
FOR rec IN getrowid LOOP
UPDATE service_tmp
SET remarks_str = rec.remarks;
END LOOP;
COMMIT;
END;
/

EXECUTE long2varchar;

Note:
Works in Oracle 9i

No comments:

Post a Comment