Skip to content

Fetch SQL results directly as text before Python type conversion #925

Backlog

Description

@carferrer

Could mssql-python provide a supported mode to fetch SQL values directly as text through ODBC, before materializing Python objects such as Decimal, float, and datetime?

Tested version: mssql-python 1.18.0.

We are evaluating a migration from PHP/PDO_SQLSRV to direct Python access. Existing consumers expect textual representations of non-NULL values. Currently, we receive native Python types and convert them back to strings, which adds substantial processing time for larger result sets.

add_output_converter() does not appear to eliminate this intermediate conversion: according to the documentation, numeric and temporal converters receive already-materialized Python objects.

Example using only fictitious data:

SELECT
    CAST(42 AS int) AS integer_value,
    CAST(123.4500 AS decimal(12,4)) AS decimal_value,
    CAST(1.23456789 AS real) AS real_value,
    CAST(1E-5 AS float) AS float_value,
    CAST('2024-01-02T03:04:05.1234567' AS datetime2(7))
        AS timestamp_value,
    CAST('2024-01-02T03:04:05.1234567+05:30' AS datetimeoffset(7))
        AS timestamp_with_offset,
    CAST(CAST(12.3400 AS decimal(12,4)) AS sql_variant)
        AS variant_value,
    N'Fictitious Unicode text: ñ' AS text_value,
    CAST(NULL AS decimal(12,4)) AS null_value;

The proposed mode would return text for supported non-NULL scalar values while preserving SQL NULL as Python None. For example:

{
    "integer_value": "42",
    "decimal_value": "123.4500",
    "timestamp_value": "2024-01-02 03:04:05.1234567",
    "null_value": None,
}

These illustrate the desired preservation of information, rather than prescribing a universal formatting convention.

Important requirements would be:

  • Opt-in behavior, leaving existing fetching behavior unchanged.
  • Conversion before creating intermediate numeric and temporal Python objects.
  • Preservation of decimal scale, temporal fractional precision, and timezone offsets.
  • Documented, precision-preserving representations for real and float.
  • Support for sql_variant values whose underlying type changes between rows.
  • Correct Unicode handling.
  • Explicit behavior for binary and unsupported types; these should not silently become lossy text.
  • Compatibility with normal fetching methods and connection reuse, without rewriting every SELECT.

In an exploratory benchmark using 1,000 rows of mixed fictitious values, a reused connection, and 100 measured reads, the median post-fetch conversion time was approximately 31 ms, compared with approximately 34 ms for execution and fetching. This suggests that avoiding the intermediate representations could be useful, although it does not establish how much faster a native text mode would be.

Explicit SQL CAST/CONVERT expressions are an alternative, but require query changes and careful type-specific formatting. A generic conversion of sql_variant to nvarchar can change formatting or discard precision.

We understand that ODBC text representations may not exactly match PDO_SQLSRV formatting. A documented, information-preserving text mode would still be valuable, and we could test compatibility separately.

Is there already a supported way to request this behavior? If not, would an opt-in text-fetch mode be a suitable enhancement?

Reference: [Custom type converters documentation](https://learn.microsoft.com/en-us/sql/connect/python/mssql-python/custom-type-converters).

Activity

  1. github-actions commented on Nov 28, 2026

    @github-actions

    Hi carferrer, thank you for opening this issue!

    Our team will review it shortly. We aim to triage all new issues within 24-48 hours and get back to you.

    If you have additional information to share, please feel free to update the issue.

    Thank you for your patience!

  2. added
    triage needed
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    💡 Enhancement🆕 Triage Neededtriage needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions