Incorrect syntax near '2021'. (102) using pyodbc to insert a datetime object into a SQL Server database

668 Views Asked by At

I am trying to insert the current datetime into a datetime2 type column of a SQL Server table:

creationdatetime = datetime.datetime.now() # 2021-11-20 21:35:40.851495

sql = ("INSERT INTO [SCHEMA].[TABLE] (\n"
        "[CreationDateTime]) \n"
        f"VALUES ('{creationdatetime}')")

cursor.execute(sql)
cursor.commit() 

I keep on getting this error:

[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '2021'. (102) (SQLExecDirectW)

Already spent like 3 hours to fix this issue....

Any help is really appreciated

Thank you

0

There are 0 best solutions below