'Insert Python Datetime into SQL Server

I'm trying to insert python DataFrame into SQL Server table but I'm getting an error when it reaches the column with Datetime entries.

# Insert Dataframe into SQL Server:
for index, row in df.iterrows():
      cursor.execute("INSERT INTO dbo.shablon (id, abbr, reg_date) values (?,?,?)", row.id, row.abbr, row.reg_date) 
conn.commit()
cursor.close()

I get this error

DataError: ('22008', '[22008] [Microsoft][ODBC SQL Server Driver]Datetime field overflow (0) (SQLExecDirectW)')

I tried to change date type inside the database into datetime and datetime2 but that didn't help. Datetime entries in python are like 2013-02-26 or NaT. NaT must be saved in the database as empty value.

UPDATE:

The problem is caused by missing values in the reg_date.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source