Skip to content

Data Type Conversion

ttk edited this page Mar 1, 2025 · 6 revisions

Python 3

Python Parameters Sent to the Database
The following table explains how Python objects passed to Cursor.execute() as parameters are formatted and sent to the driver/database.

Description Python Datatype ODBC Datatype
null None SQL_VARCHAR
boolean bool BIT
integer int SQL_TINYINT, SQL_SMALLINT, SQL_INTEGER, SQL_BIGINT
floating point float SQL_DOUBLE
decimal decimal.Decimal SQL_NUMERIC
string str SQL_VARCHAR or SQL_LONGVARCHAR
binary bytes, bytearray SQL_BINARY or SQL_VARBINARY
date datetime.date SQL_TYPE_DATE
time datetime.time SQL_TYPE_TIME
timestamp datetime.datetime SQL_TYPE_TIMESTAMP
UUID / GUID uuid.UUID SQL_GUID

SQL Values Received from the Database
The following table describes how database results are converted to Python objects.

Description ODBC Datatype Python Datatype
NULL any None
bit SQL_BIT bool
integers SQL_TINYINT, SQL_SMALLINT, SQL_INTEGER, SQL_BIGINT int
floating point SQL_REAL, SQL_FLOAT, SQL_DOUBLE float
decimal, numeric SQL_DECIMAL, SQL_NUMERIC decimal.Decimal
1-byte text SQL_CHAR str via UTF-8
2-byte text SQL_WCHAR str via UTF-16LE
binary SQL_BINARY, SQL_VARBINARY bytes
date SQL_TYPE_DATE datetime.date
time SQL_TYPE_TIME datetime.time
timestamp SQL_TIMESTAMP datetime.datetime
UUID / GUID SQL_GUID str or uuid.UUID
Clone this wiki locally