Bug report #14083

Adding up (+) variables in expression builder results in concatenation

Added by Rombert Stapel almost 9 years ago. Updated almost 9 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Expressions
Affected QGIS version:2.12.1 Regression?:No
Operating System:OS X El Capitan Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:22093

Description

If I add up two variables using the expression builder, the two values are concatenated, instead of added up. So if A = 2091 and B = 0, I get the result of A+B = "20910" (as if I used the || operator). The other operators (-, /, *) seem to work as they should. If I add up two integers, there is no problem.
I have the latest QGIS on the Mac. Version 2.12.2 is not yet available on the Mac, so I am not sure if the bug is still present there.

Schermafbeelding_2016-01-11_om_13.29.24.png - Subtracting two variables (2091-0=2091) (53.1 KB) Rombert Stapel, 2016-01-11 04:35 AM

Schermafbeelding_2016-01-11_om_13.28.46.png - Adding up two variables (2091+0=20910 (sic)) (53.6 KB) Rombert Stapel, 2016-01-11 04:35 AM

History

#1 Updated by Giovanni Manghi almost 9 years ago

  • Status changed from Open to Feedback
  • Target version deleted (Future Release - High Priority)
  • Priority changed from High to Normal

your fields are text, not integers or decimals, I would say that the results is the expected one.

#2 Updated by Nyall Dawson almost 9 years ago

  • Status changed from Feedback to Closed
  • Resolution set to invalid

Variables are always string values. You can convert them to a number using the expression functions to_real or to_int, eg 'to_real(@var1) + to_real(@var2)'

#3 Updated by Rombert Stapel almost 9 years ago

  • Status changed from Closed to Reopened

That would not explain why I can multiply, subtract and divide the same numbers without any problem. The variables in question are designated as integers, not string values. In my view this is still a bug.

#4 Updated by Nathan Woodrow almost 9 years ago

What are the types of the columns?

#5 Updated by Nyall Dawson almost 9 years ago

  • Status changed from Reopened to Closed

Wrapping the variables in to_int/to_real is the correct solution. Multiply/etc have special handling to basically have to_real forced, but that's not possible with + because it's also reused for string concatenation. Otherwise the expression '5' + '2' would incorrectly return 7 instead of 52.

Also available in: Atom PDF