Using python to query a field in a layer

551
1
Jump to solution
05-18-2018 05:46 AM
JordanMiller4
Occasional Contributor III

I am trying to query a field in a layer but it fails converting the nvarchar value '65N' to data type int. Is there a way I can prevent it from converting and simply just run a query and print the results?

Runtime error 
Traceback (most recent call last):
  File "<string>", line 7, in <module>
RuntimeError: Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Conversion failed when converting the nvarchar value '65N' to data type int.] [NEO_1.SDE.P_Meters]
‍‍‍‍

import arcpy

fc = 'Database Connections\SDE.sde\NEO_1.SDE.P_PipeSystem\NEO_1.SDE.P_Meters'
field = 'SYSTEMNUMBER'

cursor = arcpy.da.SearchCursor(fc, (field, "SYSTEMNUMBER"), """"SYSTEMNUMBER" = 009""")
for row in cursor:
    # Print the name of the different gas systems
    #
    print(row[1])
    result = arcpy.GetCount_management(fc)
    print('{} has {} records'.format(row, result[0]))
0 Kudos
1 Solution

Accepted Solutions
KevinDunlop
Occasional Contributor III

Try putting  ' around the 009.  This will convert it from a number to a string.  Otherwise 009 becomes simply 9 and you are comparing a string to a int. You can also remove the " from around SYSTEMNUMBER.

"""SYSTEMNUMBER = '009'"""

View solution in original post

1 Reply
KevinDunlop
Occasional Contributor III

Try putting  ' around the 009.  This will convert it from a number to a string.  Otherwise 009 becomes simply 9 and you are comparing a string to a int. You can also remove the " from around SYSTEMNUMBER.

"""SYSTEMNUMBER = '009'"""