Forum Moderators: open

Message Too Old, No Replies

mysql command to cut the length of data

how can i cut the length of strings that exceed the number of 255 character

         

cozzie

10:56 am on Aug 31, 2009 (gmt 0)

10+ Year Member



Hi there

I have a text column in a database table. Can anyone tell me the MySQL command that would allow me to cut the number of characters to a maximum of 255?

The table is quite large and has many pre-existing entries which have column entries of way over 255 characters so i do need to to have the column as a 'text' one rather than 'varchar', however for certain recent posts I need to limit the length to 255 characters.

I've tried loads of searches but can't find the mysql code that allows me to do this, so if anyone can help me I'd be really grateful.

thanks!

Frank_Rizzo

11:50 am on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Need to elaborate.

Do you want to truncate the column size to a max of 255?

Change all current fields to a max size of 255 (leaving current column size in place?)

Limit the size of new data inserted / updated to 255 (leaving the current column size in place?)

rocknbil

4:49 pm on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are three ways I can think of:

- Easiest, change the current field to varchar(255). This, of course, will truncate all your old data.

- Add a new varchar(255) field, and direct all new inserts to it. On reading, you will have to add some programming to get the new data as well as the old, not sure how you'd do this - maybe do it by date of the change or whether one or the other is blank/null.

- If you're a programmer, this is probably easier than truncating your data. Add programming in your input to truncate the incoming text to 255 characters.

Overall, I would say the first option may be best as it will reduce the size of a large table by quite a bit, but you will truncate existing data which may not be an option.

cozzie

1:27 am on Sep 1, 2009 (gmt 0)

10+ Year Member



thanks a lot guys for the quick response.

Its really a one off command I need which can trim some (but only some) of the data. So somethig like:

trim 'column' to maximum 255 characters WHERE topic_id > 1179

Truncating old data (either by changing column structure or via programming) isn't really an option as the old data is held within various language tags and deleting those would be a disaster.

Is there any way to do this in MySQL?

thanks a lot!