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 Answer 1
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' ), ' ' )
-
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– M-Rick2020年06月30日 19:08:00 +00:00Commented Jun 30, 2020 at 19:08
"field" || 'text'
or using the functionconcat
. 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 usearray_agg
insteadarray
.