Dylan Devotta
Dylan Devotta

Reputation: 85

Inserting DATETIME value in SQL Server using python

I have the below query that Python uses to insert values into database.

INSERT INTO Shift_Info (Login_Date,Machine_Number,Item_Number,Job_Number,Operator,Shift,LogOff_Date,Good_Parts,Total_Run_Time,Effective_Run_Time,Run_Through) 
VALUES (2020-03-16 15:42:21,'CX07',310164,5165877,21952,1,2020-03-16 15:51:06,89,525,525,12)

But get the below error on the first entry of time value at '15'. Whats the issue and any solution other than reformatting inputs?

pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '15'.")

Upvotes: 1

Views: 1205

Answers (1)

Dylan Devotta
Dylan Devotta

Reputation: 85

After reading the docs on transact-sql https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15

I just need to enclose in quotes to allow SQL Server to parse the string.

INSERT INTO Shift_Info (Login_Date,Machine_Number,Item_Number,Job_Number,Operator,Shift,LogOff_Date,Good_Parts,Total_Run_Time,Effective_Run_Time,Run_Through) 
VALUES ('2020-03-16 15:42:21','CX07',310164,5165877,21952,1,'2020-03-16 15:51:06',89,525,525,12)

Upvotes: 2

Related Questions