Saturday, 24 December 2016

Combining Columns QGIS Attribute Table


I have two columns in my QGIS attribute table. The first column contains values that aren't contained the second column and vice versa. I would like to add a new field that contains both the values from the first and second column. I thought it might be as simple as "Value 1 + Value 2) but this just gives me Null results. All values are strings.



Value 1| Value 2 | New Column
-------------------------
Bacon | | Bacon
Eggs | | Eggs
| Cheese | Cheese
| Ham | Ham

Answer



Many operators and functions in SQL (and therefore expressions) return NULL if one of the parameters was NULL


The following examples demonstrate the behavior of various operators on a layer with the columns A and B.





  • NULL + 'text'NULL

  • 'a' + 'b''ab'




  • NULL || 'text'NULL

  • 'a' || 'b''ab'





  • CONCAT(NULL, 'text')'text'

  • CONCAT('a', 'b')'ab'




  • COALESCE(NULL, 'text')'text'

  • COALESCE('a', 'b')'a'

  • COALESCE('a', NULL)'a'

  • COALESCE(NULL, NULL, 'Other')'Other'



In your case you want to work with either CONCAT or COALESCE depending on the expected behaviour with multiple / no values.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...