13

I have a field in a table that is varchar2, 4000 bytes. There are over 50000 rows. Not all rows have data in this field. Few data fields are over 255 bytes, but some are 4000. To place the table in a new application, I need to shorten the field to 255 bytes.

Is there a SQL statement that will reduce the length to 255? I realize data will be lost, that is part of the cost of the new application. The cut should be arbitrary, just stopping the data at 255 no matter the circumstance.

0

1 Answer 1

33
update b set text2 = substr(text2,1,255);

then alter table to set length of column to 255 :

alter table b MODIFY "TEXT2" varchar2(255 byte);
9
  • 9
    as there are only a few rows, I would only update the records that are too big... add WHERE LENGTH(text2)>255 Dec 7, 2011 at 9:16
  • 1
    THEORY QUESTION: I'm curious as to how this would help. at first glance I agree it should. But as I started to think about it I began to wonder. with the where, it would have to do a length first (performance hit) On EVERY record as I doubt there's an index on the length. then it has to the the substring. So it seems to me that it might actually be slower. just using the str, it does the count once for every record and it stops at 255 or end of string every time. whereas with length it has to count everything. then do the substring. Just food for thought.
    – xQbert
    Dec 7, 2011 at 17:54
  • 2
    it woudl depend if there where any UPDATE triggers on the table also.
    – ShoeLace
    Feb 24, 2012 at 6:19
  • 4
    actually you preobably want alter table b MODIFY "TEXT2" varchar2(255 CHAR); CHAR not BYTE
    – ShoeLace
    Feb 24, 2012 at 6:20
  • 2
    Agreed with Kevin. The time needed to get the length of a string is minimal, but the work needed to update a row is much greater in comparison. Jul 25, 2013 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.