5

I want to update the string length for a shapefile layer in QGIS. I've tried opening up Layer Properties and clicking edit but it won't let me change it manually. Also I've tried using the field calculator but I can't seem to get my head around it. I can create a new comment field (see "Comments_1"), but can't update the existing "Comments" to a character length of 254. The screenshot shows both windows I tried in.

enter image description here

3
  • I might suggest create a new field as you indicated, Output field type=text, Output field length=254 and in the expression section - "Comment". I am not aware how to change those properties in QGIS. I am sure you could do it in Python though.
    – Binx
    Jun 14, 2022 at 18:14
  • There is a deleted answer by @Mayo, propsing to use refactor fields. It is a completele valid solution, I don't know why OP deleted the answer...
    – Babel
    Jun 14, 2022 at 19:46
  • Refactor fields is interesting but require to save the results within a new layer. With the method proposed by Binx, the result can be updated within the same layer.
    – user35117
    yesterday

2 Answers 2

7

If you want to change the length of a field you need to use the “Refactor fields” tool.

1
  • Which is under the Processing menu, Toolbox, expand "Vector table" then double-click on "Refactor fields".
    – Malvineous
    Aug 22 at 9:48
5

As in the comment from Binx, the main bit here is to calculate a new field and use the input boxes to set the field type and length:

enter image description here

Using 'length()' in the expression will return the actual number of characters in x, not reset a field to that length: enter image description here

3
  • Nice annotations in your image!
    – Binx
    Jun 14, 2022 at 18:53
  • Note: You are limited to 10 characters for your Output field name.
    – Binx
    Jun 14, 2022 at 19:08
  • And the maximum string length in shapefile is just that 254 characters.
    – user30184
    Jun 14, 2022 at 20:36

Your Answer

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

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