-
Notifications
You must be signed in to change notification settings - Fork 8
Data Type Conversion
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 |
Note on Description Column:
The "Description" column in the table is intended to provide a generic label for the type, and does not always reflect the exact SQL Server data type name. Please refer to the SQL Server column type for precise behavior and compatibility.
Note on SQL Server's timestamp and rowversion types
- In SQL Server, the
timestamp
type is a deprecated alias forrowversion
, which is a binary type (binary(8)
) used for version-stamping table rows. It does not represent a date or time value. - The mapping table above may refer to "timestamp" as a generic type label, but this should not be confused with SQL Server's
timestamp
/rowversion
type. - At this time,
rowversion
/timestamp
columns are not mapped to Python'sdatetime.datetime
type; they should be treated as unsupported or as binary data. - For actual date/time values, use SQL Server types such as
datetime
,datetime2
,smalldatetime
, ordate
.
Caution
If you need to use SQL Server's rowversion
/timestamp
, handle these columns as binary data in your Python code. They do not carry any date or time information.
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 |