Select to view content in your preferred language

Problem with GetParameter when making an expression

589
1
12-01-2022 02:52 PM
Xitermelon
New Contributor

Hello,

I tried to make a selection according to an input date given by a user from my toolbox. However, when I use the arcpy.GetParameter, it keeps returning error 000385. If I use a specific date in the query expression, I can get a result.

Example:

date_input = arcpy.GetParameter(0)

date_select = "USER_Date_fin = timestamp 'date_input'"
arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", date_select )-------------------doesn't work

-------------------------------------------------------------------------------------------------------------------------------------------------------

date_input = arcpy.GetParameter(0)

date_select = "USER_Date_fin = timestamp '2022-11-30 00:00:00'"
arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", date_select )---------------------work

 

Tags (3)
0 Kudos
1 Reply
HannesZiegler
Esri Contributor

You are trying to pass a variable as a string - this won't work. Presumably the variable data_input stores datetime object. The Python interpreter will literally see this: "USER_Date_fin = timestamp 'date_input'". It will not see see the string as a variable and automatically read it's contents.

What you need to do is insert the actual value the date_input variable holds into the string. You can do this with string formatting. Try this:

f"USER_Date_fin = {date_input}"

Note, depending on the format of the datetime you're passing and what the query requires, you might need to work this over a little more. I didn't test this. 

 

 

0 Kudos