9

I'm trying to replace selected string values in an attribute table, eg. "Text1" for "Text2" (QGIS 1.8.0), patriculary for those rows with NULL values. I have been trying to use the replace expression (and any other that I have found) with no luck.

Are there any wildcard characters that I may be able to use in future?

2
  • 1
    check also regexp_replace() function as described in gis.stackexchange.com/questions/40637/…
    – underdark
    Dec 3, 2012 at 8:33
  • What if want to replace collumn1 based on a matrix preformed by collumn 1 and collumn 2. Something like this: collumn1 having 100 different combinations of 10 digits, 10x10=100 - matrix) for each value of the collumn2 (totalling: 1000 replaces). Collumn2 varying from '1 to 10'. For each value of collumn2 i want a substitution within these two matrix values: ![enter image description here](i.stack.imgur.com/GnGU6.png)
    – Rui
    Aug 24, 2018 at 13:29

2 Answers 2

12

You could try a case when expression:

case when "Column_1" is NULL then  replace("Column_2",'Text2','Text1') else "Column_2" end

Using the 'or' expression, you can add multiple columns with NULL-values to the code.

case when "Column_1" is NULL or "Column_3" is NULL then  replace("Column_2",'Text2','Text1') else "Column_2" end

edit:

If you want to replace NULL, forget the replace-expression (it only works for strings and NULL is no string). Try this exact code:

case when "Feature" is NULL then '12_1' else "Feature" end
7
  • Thanks for the fast reply. How would the 'case when' expression work when simply wanting to update values in a field, rather than creating a new field?
    – mastorey
    Dec 2, 2012 at 19:51
  • Just select the field you want in the Field Calculator instead of choosing "Create new field". The code stays the same.
    – GR_
    Dec 2, 2012 at 19:55
  • Thanks again. I think the problem may lie in the fact that my attribute table has but one field? I am trying to update the values of selected polygons, i.e. from a NULL value to a single alphanumeric tag. Whatever, I wonder, happenned to the 'find and replace' function of earlier QGIS releases.
    – mastorey
    Dec 2, 2012 at 20:10
  • 1
    This should do the trick: case when "Column_1" is NULL then 'Text1' else "Column_1" end
    – GR_
    Dec 2, 2012 at 20:15
  • Thanks but I'm still not getting any changes to the attribute table. Based on the code you've suggested, I'm entering the following: case when "Feature" is NULL then replace("Feature",'NULL','12_1') else "Feature" end I've also tried to express NULL as both 'NULL' and '' without success. Any further help would be very welcome and most appreciated.
    – mastorey
    Dec 2, 2012 at 20:36
-2

case when "Feature" is "TEXT 1" then 'TEXT 2' else "Feature" end

Work for my. NULL value interpretation seems to raise issues. One possible solution is to initialize the table with a neutral value different from NULL.

4
  • 2
    Could you expand on this to explain how this answers the question regarding wildcards?
    – MaryBeth
    Mar 21, 2017 at 11:54
  • Well, NULL is just a replacement for the empty fields. The problem is how QGIS sees NULL value when you use "Field Calculator." As NULL or empty field ' '. Mar 21, 2017 at 12:21
  • But on the empty fields '' can not be performed BOOLEAN operations and this is biggest problem. Mar 21, 2017 at 12:40
  • case when "GridRef" is NULL then 0 else "GridRef" end - Finally it worked. but why ... please do not ask;))) Mar 21, 2017 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.