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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
mastorey
  • 449
  • 1
  • 4
  • 19
  • 1
    check also regexp_replace() function as described in http://gis.stackexchange.com/questions/40637/how-to-easily-edit-attribute-data-using-regular-expressions – underdark Dec 03 '12 at 08: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 – Rui Aug 24 '18 at 13:29

2 Answers2

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
underdark
  • 84,148
  • 21
  • 231
  • 413
GR_
  • 1,005
  • 9
  • 20
  • 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 02 '12 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 02 '12 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 02 '12 at 20:10
  • 1
    This should do the trick: case when "Column_1" is NULL then 'Text1' else "Column_1" end – GR_ Dec 02 '12 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 02 '12 at 20:36
  • Well, that explains it. Many thanks for your help. – mastorey Dec 02 '12 at 20:57
  • After some trial and error, I also managed to get it resolved using FieldPyculator. Thanks again for all your earlier assistance – mastorey Dec 02 '12 at 23:05
-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.