I am reading a SQL Server rowversion
column into a Pandas
Dataframe. The dataframe looks like this:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 rowversion 1 non-null object
dtypes: object(1)
memory usage: 136.0+ bytes
Then create a variable
rowversion = df.iloc[0]['rowversion']
Then inject that rowverison variable into another SQL query
sql = pd.read_sql_query(
'''
SELECT *
FROM [dbo].[Table]
WHERE [RowVersion] > [%s]
ORDER BY [RowVersion] ASC
''' %(rowversion), source_engine)
df = pd.DataFrame(sql_query)
However this results in an error
[SQL Server]Incorrect syntax near '\\x00'.
That's because the query being ran is
rowversion > b'\x00']
I am guessing this is because Pandas handles binary differently from how SQL Server is expecting it. If so, how do I convert the binary back into Hex for injecting into a SQL Server query?
UPDATE:
Applying the below code seems to fix the problem, but I'm not sure if it's the correct way of fixing the issue?
rowversion = rowversion.hex()
rowversion = '0x' + rowversion