Skip to content

Data Type Conversion

Jahnvi Thakkar edited this page Feb 14, 2025 · 12 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 BIT
boolean bool BIT
integer int SQL_TINYINT, SQL_SMALLINT, SQL_INTEGER, SQL_BIGINT
floating point float SQL_REAL, SQL_FLOAT, SQL_DOUBLE
decimal decimal.Decimal SQL_DECIMAL, SQL_NUMERIC
string str SQL_VARCHAR or SQL_LONGVARCHAR (2)(3)
binary bytes, bytearray SQL_VARBINARY or SQL_LONGVARBINARY (3)
date datetime.date SQL_TYPE_DATE
time datetime.time SQL_TYPE_TIME
timestamp datetime.datetime SQL_TYPE_TIMESTAMP
UUID / GUID uuid.UUID SQL_GUID

If the driver supports it, SQLDescribeParam is used to determine the appropriate type. If it is not supported, SQL_VARCHAR is used.

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 (1)
2-byte text SQL_WCHAR str via UTF-16LE (1)
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 (2)
Clone this wiki locally