Select to view content in your preferred language

CAST does not work in SearchCursor Queries?

2001
11
Jump to solution
07-17-2018 02:39 PM
DougBrowning
MVP Esteemed Contributor

I thought a found a slick way to compare on a number in a string field. 

CAST(SoilDepthLower AS INTEGER) < 70

This works in a ArcMap definition query just fine. 

But when I try to use it in a SearchCursor Query it says Invalid SQL Statement.

                    whereClause = "CAST(SoilDepthLower AS INTEGER) < 70"
                    keyField1 = arcpy.ListFields(tableLayer)[1].name
                    keyField2 = arcpy.ListFields(tableLayer)[2].name
                    fieldList = ["SoilDepthLower", keyField1, keyField2]
                    with arcpy.da.SearchCursor(tableLayer, fieldList, whereClause) as cursor:

I have tried smallint, double, etc - using both uppercase and lower case.

This help page says it is supported  http://pro.arcgis.com/en/pro-app/help/mapping/navigation/sql-reference-for-elements-used-in-query-ex...

What is the issue?

Thanks

0 Kudos
11 Replies
DougBrowning
MVP Esteemed Contributor

It is a python in_memory table (not a FC) which in my experience acts just like a GDB.  When I make a test copy into a GDB and use my cast query in a Definition Query it works great.  Which should simulate the interface directly.

I have tried int, INT, Interger, INTEGER, SHORTINT, and a few others.

thanks

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you feel one of these responses answered your question, please mark it so to close out the question.  Otherwise, I will close it as "assumed answered" later.

0 Kudos