Skip to content
Advertisement

Why is ODBC Driver 17 for SQL Server converting strings to dates automatically and how can I stop this?

I’m moving an app that was previously running on windows 10 to a docker container with python3.6 linux base image. One of the necessary changes was changing the driver used in sql connection string from “SQL Server” to ODBC Driver 17 for SQL Server, because I have to use unixodbc-dev. I installed msodbcsql17 and mssql-tools via my Dockerfile, and I execute a query via an sqlalchemy engine that retrieves values from a column of sql type “date”. With the SQL Server driver, these dates get converted to strings (which is what the code expects), but with ODBC Driver 17 for SQL Server, they are returned as dates. I’m using pyodbc==4.0.25 and SQLAlchemy==1.3.5.

Advertisement

Answer

The legacy “SQL Server” ODBC driver hasn’t been enhanced since SQL Server 2000, long before the newer date data type (and other temporal types) was introduced with SQL Server 2008. The driver will return unrecognized types as strings instead of the native type.

If the native type is a breaking change for the app code, the correct solution is to use proper types in app code and the newer driver. About all you can do is use the legacy driver in the interim.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement