6

I am using QGIS 3.4 and I want to change all my 0 values to NULL. How can I do that in the attribute table?

2

3 Answers 3

8
  • select all your '0' rows -> Go to 'Select by expression' and select all rows where attribute = 0
  • Go to 'Field calculator' and check 'Update existing field', 'Only update selected features' and select your field = NULL
6

After selecting the layer, use this script in the QGIS's Python Editor (Ctrl+Alt+P):

from qgis.utils import iface

layer = iface.activeLayer()

idx = range(layer.fields().count())

layer.startEditing()

for f in layer.getFeatures():
    attr_map = {i: NULL for i in idx if f[i] == 0}
    layer.changeAttributeValues(f.id(), attr_map)
    
layer.commitChanges()

This script changes all zeros in all columns to NULL.

enter image description here

3

Or if you do not want to select all features manually or dont want to use PyQGIS, you can simply use if() or case when, like:

if("field" = 0,NULL,"field")

or

case
when "field" = 0 then NULL
else "field"
end

Don't forget to set the checkmark on "Update existing field":

enter image description here

Your Answer

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

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