Skip to content

Binary Data Padding - extra characters getting added #226

@bewithgaurav

Description

@bewithgaurav

Describe the bug

Binary data retrieved from VARBINARY columns contains extra null bytes (\0) at the end, making the returned data longer than what was originally stored.

To reproduce

import mssql_python as sql

conn = sql.connect("your_connection_string")
cursor = conn.cursor()

cursor.execute("CREATE TABLE #test_binary (data VARBINARY(50))")
cursor.execute("INSERT INTO #test_binary VALUES (?)", (b"123!@#",))

cursor.execute("SELECT data FROM #test_binary")
result = cursor.fetchone()[0]

print(f"Expected: {b'123!@#'}")           # b'123!@#' (6 bytes)
print(f"Actual: {result}")                # b'123!@#\\x00\\x00\\x00' (9 bytes)
print(f"Expected length: 6")
print(f"Actual length: {len(result)}")    # 9 instead of 6

Expected behavior

The retrieved binary data should be exactly b"123!@#" without any trailing null bytes (\0).

Further technical details

Python version: 3.10+
SQL Server version: SQL Server 2019/2022
Operating system: All supported platforms

Additional context
This affects binary data comparisons and applications that expect exact byte-for-byte retrieval. The extra null bytes cause data integrity issues and failed assertions in tests.

Metadata

Metadata

Assignees

Labels

Triage DoneIssues that are triaged by dev team and are in investigation.bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions