-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deciphering SQL_VARIANT values #307
Comments
I have the same issue when trying to select data structure from database. |
I also have problem when I tried to do a SELECT over a SQL_VARIANT Column. Using TDS version 4.2 I can make the SELECT, but I can't use v4.2 with Django 2.2 because it complains with Datetime Fields. By using pyodbc.connect(...).add_output_converter(-150, sql_variant_handler), where sql_variant_handler is a function I'm able to retrieve the SQL_VARIANT value, but such value is a encrypted somehow. I'm currently trying to find a way to get the real data (integer values) to no avail. |
@jcfernandez-890825 - If your output converter function is receiving four bytes and you want to convert them to an >>> struct.unpack('i', b'Gord')
(1685221191,) |
I know it won't be ideal for all use cases but if your workload is not intensive or overly complex and you're trying to retrieve data in SQL Server from something like a system property where the value is of type sql_variant, I had luck doing the type conversion in the SQL statement so that when it was returned to pyodbc it was already in a compatible type:
|
@gordthompson Do you know if this request was ever worked on? |
@hummingbird1989 - Not that I'm aware of. |
That's because pyODBC Is generic and variant types are specific to SQL Server, however you can obtain the base type of a variant using SQL: https://learn.microsoft.com/en-us/sql/t-sql/functions/sql-variant-property-transact-sql |
A question on Stack Overflow regarding pyodbc and SQL_VARIANT got me curious to see if an output converter function might be feasible. I confirmed that, without an output converter function, pyodbc would throw
when I tried to retrieve a SQL_VARIANT column. When I added a little function to simply return the bytes (so I could see what we had to work with) ...
... I found that just the data bytes were returned, without the metadata to describe what type of value it was. That is, under Python 3.6 the above code prints
so we apparently cannot differentiate between the values in the two rows:
'Gord'
and1685221191
.The Microsoft document Mapping Data Types (ODBC) suggests that there is a way to get that information (SQL_CA_SS_VARIANT_TYPE) so perhaps pyodbc can make it available to the output converter function.
The text was updated successfully, but these errors were encountered: