I want to select records within a period of date. However, the corresponding data type in database is like '2017-11-13-05-36-25'. I need to convert it to datetime format first. The following SQL query is made using pyodbc:
connection = pyodbc.connect("Driver={\
ODBC Driver 13 for SQL Server};\
Server=server1,\
100;Database=my_db;\
Uid=abc;\
Pwd=123;\
Encrypt=yes;\
TrustServerCertificate=no;\
Connection Timeout=30;")
cmd = r'SELECT startDate FROM table1 where \
CONVERT (datetime, startDate, 120) between \
CONVERT(datetime, "2017-10-8 00:00:00", 120) and \
CONVERT(datetime,"2017-11-7 00:00:00", 120)'
a = pd.read_sql(cmd, con = connection)
After execution, I received the following error:
Error: ('42S22', "[42S22] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name '2017-10-8 00:00:00'. (207) (SQLExecDirectW)")
How can get the correct result?