About Me

Database and GIS Consultant.

Monday, October 19, 2020

Python - Esri's ArcPy - Using Where_Clause - Examples

Environment: ArcCatalog 10.5.1, Oracle GDB, Python 2.7, Esri's ArcPy module


Examples of using where clause ("where_cls")

Example # 1: Create a blank feature class at the destination with the same structure as source. So I use a non-matching condition (-1), so a blank feature class is created:-

 

import arcpy

out_loc = r"C:\SDE_Connections\SCOTT@TODB.sde"
in_fc = r"C:\SDE_Connections\SCOTT@FROMDB.sde\TEST_FC"
out_fc = "TEST_FC"

C_Field = arcpy.AddFieldDelimiters(in_fc, "ID")
where_cls = C_Field + "= -1"
arcpy.FeatureClassToFeatureClass_conversion(in_fc, out_loc, out_fc, where_cls, config_keyword="VECTOR_DB")


Example # 2: Create a feature class whose column values has a length of only 5 characters from the source.


import arcpy

out_loc = r"C:\SDE_Connections\SCOTT@TODB.sde"
in_fc = r"C:\SDE_Connections\SCOTT@FROMDB.sde\TEST_FC"
out_fc = "TEST_FC"

C_Field = arcpy.AddFieldDelimiters(in_fc, "POSTCODE")where_cls = "char_length("+C_Field+") = 5"

arcpy.FeatureClassToFeatureClass_conversion(in_fc, out_loc, out_fc, where_cls)

Hope it helps !