My favorites | Sign in
Project Home Downloads Wiki
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 204: SQL Server DATE columns come back as 'str' rather than 'datetime.date'
1 person starred this issue and may be notified of changes. Back to list
Status:  NoFix
Owner:  ----
Closed:  Sep 2011


 
Reported by danias...@gmail.com, Aug 24, 2011
According to the documentation (https://code.google.com/p/pyodbc/wiki/DataTypes) the SQL Server DATE type is mapped to datetime.date, however I seem to be getting back strings instead.

Consider the following table definition in SQL Server 2008 (SP1) [10.0.2531.0 (X64)]:
CREATE TABLE dbo.Contract(
    id INT NOT NULL,
    name VARCHAR(23) NOT NULL,
    some_date DATE NULL,
    write_time DATETIME NOT NULL)

Accessed with the following code:
import pyodbc
from pprint import pformat
db = pyodbc.connect("DRIVER={SQL Server};SERVER=.\\SQLEXPRESS;DATABASE=scratch;Trusted_Connection=yes")
print pformat(db.cursor().execute("select * from Contract").description)

The cursor description is:
(('id', <type 'int'>, None, 10, 10, 0, False),
 ('name', <type 'str'>, None, 23, 23, 0, False),
 ('some_date', <type 'unicode'>, None, 10, 10, 0, True),
 ('write_time', <type 'datetime.datetime'>, None, 23, 23, 3, False))

Note that the some_date column is indicated as type unicode string, however, in the database this column is defined as DATE.

(On the other hand, the write_time column is correctly mapped to datetime.datetime.)

pyodbc 2.1.8 tested on Windows 7 and RedHat Linux.
Sep 13, 2011
Project Member #1 mkleehammer
This is caused by the driver.  On Windows, you need to use DRIVER={SQL Server Native Client 10.0} for a driver that understands the ODBC date type.

I don't know what would be required for FreeTDS.

Status: NoFix

Powered by Google Project Hosting