0

I've created a table like this :

create table tbl(
address varchar(5000));

If i want to insert data in to the field address, if the length of data inserted exceeds the length of the field address. Then first truncate the the data and insert it in to address. please give me the correct query for the above problem....

2 Answers 2

2

your insert/update query should probably call a method truncate which looks like this:

public static String truncate(String value, int length)
{
  if (value != null && value.length() > length)
    value = value.substring(0, length);
  return value;
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

I've tested on my mysql (5.5.8), create table as follows:

create table test ( a varchar(10) );

then insert

insert into test(a) values('1234567890112');

then select

select * from test;

gives me 1234567890

I think mysql will automatically truncate anything bigger you pass in to the field, so you don't need to worry, unless you need to log it in your application level everytime that happens

1 Comment

Yes you'r right, but you can too configure MySQL do not truncate and message an error when data is too long then field length, I have this situation now and looking for that config to disable it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.