I am sure this is probably a simple syntax fix but I receive invalid syntax errors during the Select Analysis when trying to select records where the date field in my file geodb (ISS_CST) is greater than the date variable. I have tried a couple different lines but they do not work.

dayDateStr = '120621'
dayDate10DaysAgo = datetime.datetime.strptime(dayDateStr, "%m%d%y") - timedelta(days=10)
print (dayDate10DaysAgo)
#arcpy.Select_analysis(warningsSel2, warningsSel2a, "!ISS_CST! >" dayDate10DaysAgo)
arcpy.Select_analysis(warningsSel2, warningsSel2a, "ISS_CST > date" +dayDate10DaysAgo+"")

>> 2021-11-26 00:00:00

File "weatherloader_Test_Warnings.py", line 279, in ProcessWarnings arcpy.Select_analysis(warningsSel2, warningsSel2a, "ISS_CST > date" +dayDate10DaysAgo+"") TypeError: cannot concatenate 'str' and 'datetime.datetime' objects

If I use the line currently commented out the error is:

File "weatherloader_Test_Warnings.py", line 278 arcpy.Select_analysis(warningsSel2, warningsSel2a, "!ISS_CST! >" dayDate10DaysAgo) ^ SyntaxError: invalid syntax

  • 1
    Edit your question and show an example of what dayDateStr would actually be.
    – Hornbydd
    Commented Dec 7, 2021 at 17:36
  • Sure. I have run the script where I do a print (dayDate10DaysAgo) and the date is correct. I think it is solely due to the select analysis expression.
    – Andrew
    Commented Dec 7, 2021 at 17:41
  • You should always use str.format() to format the exact query string in the format expected. The expected format varies by data source type, so you need to specify that, too. Please Edit the question so that it reflects both the data source type (shapefile, file geodatabase,...) and the exact contents of the where_clause.
    – Vince
    Commented Dec 7, 2021 at 18:33
  • Yes file geodatabase. ISS_CST is a date field. Variable format is 2021-11-26 00:00:00.
    – Andrew
    Commented Dec 7, 2021 at 18:50
  • 2
    As Vince suggested about the edits, do provide a mostly intact script. Python is not strict and we cannot tell what a variable is without explicit knowledge or guessing. For example in your snippet, your print dayDate10DaysAgo before you define it, makes reading it difficult. Also include your SyntaxError message. You have plenty of space, use it!
    – RomaH
    Commented Dec 7, 2021 at 19:05

1 Answer 1


I converted the date variable (dayDate10DaysAgo) to string then comprised a new where_clause. It works now.

dayDate10DaysAgo = datetime.datetime.strptime(dayDateStr, "%m%d%y") - timedelta(days=10)
dayDate10DaysAgoStr = str(dayDate10DaysAgo)
where_clause = "ISS_CST > date '" + dayDate10DaysAgoStr + "'"
arcpy.Select_analysis(warningsSel2, warningsSel2a, where_clause)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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