Skip to content

Commit a95c9f9

Browse files
committed
Merge branch 'datetime_conversion_error' of https://github.com/arvis108/mssql-python into datetime_conversion_error
2 parents 519fc62 + 8a9befe commit a95c9f9

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

tests/test_004_cursor.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10743,7 +10743,90 @@ def test_datetime_string_parameter_binding(cursor, db_connection):
1074310743
finally:
1074410744
drop_table_if_exists(cursor, table_name)
1074510745
db_connection.commit()
10746-
10746+
10747+
def test_date_string_parameter_binding(cursor, db_connection):
10748+
"""Verify that date-like strings are treated as strings in parameter binding"""
10749+
table_name = "#pytest_date_string"
10750+
try:
10751+
drop_table_if_exists(cursor, table_name)
10752+
cursor.execute(f"""
10753+
CREATE TABLE {table_name} (
10754+
a_column VARCHAR(20)
10755+
)
10756+
""")
10757+
cursor.execute(f"INSERT INTO {table_name} (a_column) VALUES ('string1'), ('string2')")
10758+
db_connection.commit()
10759+
10760+
date_str = "2025-08-12"
10761+
10762+
# Should fail to match anything, since binding may treat it as DATE not VARCHAR
10763+
cursor.execute(f"SELECT a_column FROM {table_name} WHERE RIGHT(a_column, 10) = ?", (date_str,))
10764+
rows = cursor.fetchall()
10765+
10766+
assert rows == [], f"Expected no match for date-like string, got {rows}"
10767+
10768+
except Exception as e:
10769+
pytest.fail(f"Date string parameter binding test failed: {e}")
10770+
finally:
10771+
drop_table_if_exists(cursor, table_name)
10772+
db_connection.commit()
10773+
10774+
def test_time_string_parameter_binding(cursor, db_connection):
10775+
"""Verify that time-like strings are treated as strings in parameter binding"""
10776+
table_name = "#pytest_time_string"
10777+
try:
10778+
drop_table_if_exists(cursor, table_name)
10779+
cursor.execute(f"""
10780+
CREATE TABLE {table_name} (
10781+
time_col VARCHAR(22)
10782+
)
10783+
""")
10784+
cursor.execute(f"INSERT INTO {table_name} (time_col) VALUES ('prefix_14:30:45_suffix')")
10785+
db_connection.commit()
10786+
10787+
time_str = "14:30:45"
10788+
10789+
# This should fail because '14:30:45' gets converted to TIME type
10790+
# and SQL Server can't compare TIME against VARCHAR with prefix/suffix
10791+
cursor.execute(f"SELECT time_col FROM {table_name} WHERE time_col = ?", (time_str,))
10792+
rows = cursor.fetchall()
10793+
10794+
assert rows == [], f"Expected no match for time-like string, got {rows}"
10795+
10796+
except Exception as e:
10797+
pytest.fail(f"Time string parameter binding test failed: {e}")
10798+
finally:
10799+
drop_table_if_exists(cursor, table_name)
10800+
db_connection.commit()
10801+
10802+
def test_datetime_string_parameter_binding(cursor, db_connection):
10803+
"""Verify that datetime-like strings are treated as strings in parameter binding"""
10804+
table_name = "#pytest_datetime_string"
10805+
try:
10806+
drop_table_if_exists(cursor, table_name)
10807+
cursor.execute(f"""
10808+
CREATE TABLE {table_name} (
10809+
datetime_col VARCHAR(33)
10810+
)
10811+
""")
10812+
cursor.execute(f"INSERT INTO {table_name} (datetime_col) VALUES ('prefix_2025-08-12T14:30:45_suffix')")
10813+
db_connection.commit()
10814+
10815+
datetime_str = "2025-08-12T14:30:45"
10816+
10817+
# This should fail because '2025-08-12T14:30:45' gets converted to TIMESTAMP type
10818+
# and SQL Server can't compare TIMESTAMP against VARCHAR with prefix/suffix
10819+
cursor.execute(f"SELECT datetime_col FROM {table_name} WHERE datetime_col = ?", (datetime_str,))
10820+
rows = cursor.fetchall()
10821+
10822+
assert rows == [], f"Expected no match for datetime-like string, got {rows}"
10823+
10824+
except Exception as e:
10825+
pytest.fail(f"Datetime string parameter binding test failed: {e}")
10826+
finally:
10827+
drop_table_if_exists(cursor, table_name)
10828+
db_connection.commit()
10829+
1074710830
def test_close(db_connection):
1074810831
"""Test closing the cursor"""
1074910832
try:

0 commit comments

Comments
 (0)