#StackBounty: #python #sql-server #sqlalchemy SQLAlchemy and SQL Server Datetime field overflow

Bounty: 50

I’m using SQLAlchemy to connect to a SQL Server database.

I’m trying to insert an object into a table from my python script and it’s failing. I’m receiving the error:

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

It looks like this is being caused by the following datetime object:

datetime.datetime(214, 7, 21, 0, 0)

… that’s the 21st July 214

The corresponding date time field in the SQL Server table is of type datetime2.

It looks like the conversion from python/SQLAlchemy to SQL Server isn’t adding a ‘0’ to beginning of the year value. I’ve confirmed this by the fact that I can manually add this date to the SQL Server using an INSERT statement with and without the leading ‘0’.

Is there a way to force the year part of the date into the correct format? Or is this being caused by something else?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.