Skip to content
Advertisement

Getting a syntax error when trying to execute this SQL query in Python

I’m trying to get the script to automatically set the query start time as 6am yesterday (and will set the end time as 6am today once I figure out the Start time error) but I am getting a syntax error (102, b”Incorrect syntax near ’06’.DB-Lib error message 20018, severity 15:nGeneral SQL Server error: Check messages from the SQL Servern”).

If I take out the “””+StartTime+””” and change to ‘20211212 07:00:00.000’ then it works but I need a way for the script to keep track of the current date rather than going back and changing every day.

Advertisement

Answer

Consider running parameterization in Python passed into a prepared SQL statement without declaring @ params. Also, keep all variables in datetimes without any string conversion.

Not quite sure what your date ranges require given 6 AM and 7 AM confusion, below calculates range from yesterday at 6 AM to today at 5:59:59 AM (for 24 hours). Adjust as needed. Finally, final WHERE is moved into subquery.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement