Bug report #5244
Expressions: tostring() does not work as expected when the argument is evaluated to NULL
|Affected QGIS version:||master||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:||fixed|
|Crashes QGIS or corrupts data:||No||Copied to github as #:||14979|
As far as expression uses 3 logic states (true, false, null) the only way to print strings concatenated to a field is to check if the field is NULL using a CASE statement.
IMHO would make sense to change the tostring() function to return an empty string when the argument is a NULL value.
Maybe would be even better to add an overload of the function tostring(val, defaultval) where the second arg is the default value returned in case of NULL.
#2 Updated by Nathan Woodrow over 7 years ago
I don't like the idea of treating NULL as an empty string. Empty strings are still valid data where as NULL is nothing. NULL might mean N/A for my data vs '' having some meaning. Treating them the same could have bad side effects for people trying to label, style, their data.
You no longer need to use CASE and can use
coalesce(COLA, ''). You can also use the new
format function which will auto convert to strings
format('%1 %2', coalesce(COLA, ''), coalesce(COLB, ''))