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
asked Jun 30, 2020 at 16:50
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. 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' ), ' ' )
answered Jun 30, 2020 at 17:33
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. Commented Jun 30, 2020 at 19:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.