Bobspadger / pyodbc

Automatically exported from code.google.com/p/pyodbc
MIT No Attribution
0 stars 0 forks source link

SQL Server DATE columns come back as 'str' rather than 'datetime.date' #204

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
According to the documentation (http://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.

Original issue reported on code.google.com by danias...@gmail.com on 24 Aug 2011 at 11:38

GoogleCodeExporter commented 9 years ago
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.

Original comment by mkleehammer on 13 Sep 2011 at 10:14