Select to view content in your preferred language

Testing for Null Values

880
1
06-21-2011 08:40 AM
BenYoungs
Deactivated User
All,

I have a script that is building feature layers based on a SQL expression evaluating an attribute combination.

I am using:
arcpy.MakeFeatureLayer_management(<feature class>, <new layer name>, <whereclause>)

I am trying to find conditions where attribute x is a valid string value (not null, not an empty space, not a placeholder value, etc.) and attribute y is not a valid string value (null, an empty space, etc.).

I can build out an expression in ArcMap and it returns exactly the records I am looking for using:

"[Attribute X] Is Not Null AND [Attribute X] <> "" AND [Attribute X] <> " " AND [Attribute Y] Is Null OR [Attribute Y] =  "" OR [Attribute Y] = " ""

However, when I build that expression to run from the Python script, It will still pull in the records that I am looking for but also some records with null Attribute X values:

"[Attribute X] Is Not Null AND [Attribute X] <> \"\" AND [Attribute X] <> \" \" AND [Attribute Y] Is Null OR [Attribute Y] =  \"\" OR [Attribute Y] = \" \""

It seems like the expression is exactly the same but I am getting different results. Is there a better way to test for null values in an attribute table? Did I screw up the SQL somehow?

Thanks in advance.
Tags (2)
0 Kudos
1 Reply
ChrisSnyder
Honored Contributor
"where attribute x is a valid string value (not null, not an empty space, not a placeholder value, etc.) and attribute y is not a valid string value (null, an empty space, etc.)."

I think this is how I would write it (you shouldn't need all the square barackets and /s):

"(X IS NOT NULL OR X not in (""," ")) AND (Y IS NULL OR Y in ("", " "))"

Note this is already in string format ready to plop into the MakeFeatureLayer tool.
0 Kudos