How to make a varchar2 field shorter in Oracle? - oracle

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.

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);

Related

Why can't I trim a column of type CHAR?

Like the title says, if a create a table in my DB :
CREATE TABLE TEST
(
FIELD CHAR(20 CHAR) NULL
)
NOLOGGING
NOCOMPRESS
NOCACHE;
Insert this :
Insert into TEST
(FIELD)
Values
('TEST -here are blank spaces- ');
COMMIT;
Then i run the following statement :
UPDATE TEST SET FIELD = TRIM(FIELD);
COMMIT;
but the field still has blank spaces, notice that if I change the data type to varchar2, it works ... does anyone know why?
Thanks!
char is a fixed width data type. A char(20) will always and forever have a length of 20. If you try to insert a shorter string, it will be padded with spaces to the fixed width length of the field. So
UPDATE TEST SET FIELD = TRIM(FIELD);
removes the spaces due to the trim function, then adds them back because the string that gets written has to be exactly 20 bytes long.
Practically, there is almost never a case to use char. You're almost always better off with a varchar2. varchar2 is a variable length data type so there is no need for the database to append the spaces to the end.

i want to change CLOB of size in my oracle tables

I have a table with CLOB column but I want to modify the CLOB of length >>
I executed this command "Alter table TableName (flddata CLOB length 100 Gb);
but i there is an error accorded "Invalid alter table option"
I want to increase the size of the field, how is that??
Thanks in advance
A clob column has no limit in the specification of the type. So, when you define a column as CLOB, it has no size associated the same as other string types ( char, varchar2 ).
However, the limit of what you can store there is defined by this:
CLOB Maximum size: (4 GB - 1) * DB_BLOCK_SIZE initialization parameter (8 TB to 128 TB)

How to insert a string of size more than 4000 bytes into table column of type CLOB using SqL ldr

I am trying to load a string of length more than 4000 into table of column type CLOB. I know we can do this using ananymous block. But how can I use this block in control file?
You need to provide the max size of the clob as following:
LOAD DATA
INFILE <your_filename>
INTO TABLE <your_table_name>
FIELDS TERMINATED BY '<your_separator>'
TRAILING NULLCOLS
(
id,
<your_clob_column> CHAR (6000), -- max value of your clob col, default is 255
other_fields
)
see the default is 255 so it will throw an error if you do not specify the size and load the data with length > 255 so It is better to always use size as mentioned above.
Cheers!!

how to check the size of input to avoid exceed DB column limit

I have an input field of my page with size=8.
And in the DB, the corresponding column is VARCHAR2(8).
But if I input a string of length 8 with a special ascii character in the field, I will get the following exception.
ORA-12899: value too large for column xxxx (actual: 10, maximum: 8)
I'm trying to catch this in the validator, I check myString.getBytes().length which is also 8.
I know one solution is on DB side that change the column to VARCHAR2(8 CHAR).
Is there another solution that I can check this in the controller?
The error is telling you that you've given 10 bytes but the column only allows 8. I am assuming it's bytes because of your use of the Chinese character set. So, I believe that the column was created as if it were VARCHAR2(8 byte).
If you describe the table, you'll see what's going on. Compare that describe with a describe of this one:
create table x (a varchar2(30), b varchar2(30 byte), c varchar2(30 char));
The code you are executing to obtain the number of bytes is almost correct. Instead of:
myString.getBytes().length /* this probably returns 8 */
you need to execute this:
myString.getBytes("UTF-8").length /* this probably returns 10 */
This should help you, this will return the actual size in Bytes.
SELECT LENGTHB ('é')
FROM DUAL;
Above will return 2. So whatever character you are using, you can specify something like MY_VARCHAR_FIELD VARCHAR2(2 BYTES)

Change Character Allowance

I have a VARCHAR field that only allows 12 characters max. How do I change character allowance to 9 or 15 for example?
Google succeeds in telling me what the max number of characters in VARCHAR in any given version of Oracle database. I know, I get that. I just want to ALTER the column character allowance within that range.
alter table table_name MODIFY (column_to_change varchar(new size))
ALTER TABLE tbl_name MODIFY col_name column_definition;
So if you have:
CREATE TABLE table_name (
value VARCHAR2(12)
);
Then you can do:
ALTER TABLE table_name MODIFY value VARCHAR2(15 BYTE);
and the column will have a capacity of 15 bytes.
Or:
ALTER TABLE table_name MODIFY value VARCHAR2(9 CHAR);
and the column will have a capacity of 9 characters.

Resources