[Qgis-user] Field calculator - converting string to int with possible NULL values

2021-05-14 Thread David Strip

  
  
I have a string field I wish to convert to int. A small subset of
the records contain NULL.
I tried the following in the field calculator:

case
  when ("bb_usage" IS NULL) then 0
  else to_int("bb_usage"")
  end

When I look at the preview, the result is correct whether or not the
field contains int or NULL. But when I try to run it, I get an
error:
 
        

I also tried

case
  when ("bb_usage" IS NULL) then 0
  when  ("bb_usage" IS NULL) then to_int("bb_usage")
  end

and get the same error.

I tried
if ("bb_usage" IS NULL, 0, to_int("bb_usage"))

same error.

I know there are workarounds, but why don't any of these work? Why
does to_int get evaluated on the NULLs?
  

___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


Re: [Qgis-user] Field calculator - converting string to int with possible NULL values

2021-05-14 Thread David Strip

  
  
To answer my own question - 
My problem occurred in a layer that was joined to a csv file. I had
"NULL" values where there was no matching join field, as one might
expect.
But it turns out that the csv file had the string "null" in an
otherwise integer column for missing entries. Thus, when I thought
the field calculator was rejecting NULL values, it was actually
rejecting the "null" string.

Sorry for the distraction.

On 5/14/2021 4:21 PM, David Strip wrote:

  
  I have a string field I wish to convert to int. A small subset of
  the records contain NULL.
  I tried the following in the field calculator:
  
  case
when ("bb_usage" IS NULL) then 0
else to_int("bb_usage"")
end
  
  When I look at the preview, the result is correct whether or not
  the field contains int or NULL. But when I try to run it, I get an
  error:
   
          
  
  I also tried
  
  case
when ("bb_usage" IS NULL) then 0
when  ("bb_usage" IS NULL) then to_int("bb_usage")
end
  
  and get the same error.
  
  I tried
  if ("bb_usage" IS NULL, 0, to_int("bb_usage"))
  
  same error.
  
  I know there are workarounds, but why don't any of these work? Why
  does to_int get evaluated on the NULLs?
  
  
  ___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user




  

___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user