1

I was wondering if anybody could help me. Im trying to create a tool within ArcMap using a python script. Within the script i've been trying figure out how to change it so that I only need to get the numerical value from GetParameterAsText. So that for the tool i do not need to include the whole SQL expression just have the user input a value.

This the last method that I have used

wf_value = arcpy.GetParameterAsText(4)

wf_expression = ' "AGE_18_64"' + ">=" + "'%'" %wf_value

arcpy.Select_analysis(counties, workforce, wf_expression)

ERROR MESSAGE: ExecuteError: ERROR 000358: Invalid expression 3000 Failed to execute (Select)

As I'm guessing this way doesn't work as the wf_value is a float not a string but I can't seem to get it to work whichever way I've tried. Any help or suggestions would be great! Thanks!

3

I'm not by a computer where I can try this within ArcMap, but try:

wf_expression = '"AGE_18_64"' + " >= " + str(wf_value)

or, using str.format():

wf_expression = '"AGE_18_64" >= {0}'.format(wf_value)

Try that if wf_value is supposed to be a number in the sql query (you don't generally need to enclose numeric values in quotes). If however it could be a number or text (including empty string or just a space or something, then try:

wf_expression = '"AGE_18_64"' + " >= '" + str(wf_value) + "'"

or using the str.format() option it would be:

wf_expression = '"AGE_18_64" >= \'{0}\''.format(wf_value) 

Basically, if your parameter is coming in as a number, you may need to cast it as a string to concatenate it with other strings.

If those are giving you issues, you might also try changing up the quoting on your age field. Depending on your data source will determine how you need to enclose your field names, the help documentation should be pretty thorough on that if you open up a layer properties dialog in ArcMap and go to the query builder on the definition query tab and click the help button within that dialog. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.