1

I have a STRING field with values. When I want to apply some additional text after each value using an array function, I get the text added to the end of the whole initial string instead of each value. I tried space separated values, coma separated values, coma separated values with spaces but nothing is working.

Values can be numbers or letters, like 1 5 XY for example.

I applied the following expression in a virtual field calculator:

array_to_string( array_foreach( array("values"),@element || 'sometext' ), ' ' )

QGIS returns me:

1 5 XYsometext

instead of:

1sometext 5sometext XYsometext
1
  • 1
    Unfortunately, your example is not clear. What are you trying to achieve? According to the expression in your example, you can add some text to a single field just using "field" || 'text' or using the function concat. The expression is correct. If your goal is to achieve the result you are showing in your example, considering the different values as different attributes in the same field, you should use array_agg instead array.
    – Val P
    Commented Jun 30, 2020 at 17:32

1 Answer 1

3

By calling array("values") you are basically creating a 1 element array containing the entire string.

Instead, either the field is an array and you can use it directly, or it is a string and you need to create the array first

array_to_string( array_foreach( string_to_array( '1 2 XY',' ',''),@element || 'sometext' ), ' ' )
1
  • It did work. Strangely, I had to add comas. I couldn't get results using spaces. But that's OK with that. Thank you.
    – M-Rick
    Commented Jun 30, 2020 at 19:08

Your Answer

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

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