Skip to content

Datetime conversion error #234

@arvis108

Description

@arvis108

Describe the bug

mssql_python throws a datetime conversion error when performing string-to-string comparisons in SQL WHERE clauses, even though no datetime conversion should be involved. The same query works fine with pymssql and pyodbc.

The error occurs when passing a string parameter to compare against a string result from SQL functions like RIGHT(). The library incorrectly attempts datetime conversion instead of treating it as a simple string comparison.

To reproduce

test_mssql-python.py (fails):

import mssql_python

# Establish a connection
connection_string = ""
connection = mssql_python.connect(connection_string)
cursor = connection.cursor()
date_str = '2025-08-12'
# This fails with datetime conversion error
cursor.execute("SELECT a_column FROM a_table WHERE RIGHT(a_column,10) = ?", (str(date_str)))
rows = cursor.fetchall()

for row in rows:
    print(row)

connection.close()

Expected behavior

The query should execute successfully since it's a simple string-to-string comparison. RIGHT(a_column,10) returns a string, and the parameter is a string, so no datetime conversion should occur.

Workaround: The query works when explicitly casting the parameter:

cursor.execute("SELECT a_column FROM a_table WHERE RIGHT(a_column,10) = cast(? as nvarchar(10))", (str(date_str)))

Further technical details

Python version: 3.13.7
Operating system: Windows 10

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtriage doneIssues that are triaged by dev team and are in investigation.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions