Home > OS >  Python pymssql Select Sequence Number Returning Strange Value
Python pymssql Select Sequence Number Returning Strange Value

Time:10-19

I'm busy querying a SQL Server DB with Python's pymssql and am getting a strange result from the following query

cursor = conn.cursor()
cursor.execute("select current_value from sys.sequences where name = 'testSequence'")
currentValue = cursor.fetchone()[0]
cursor.close()
print(currentValue)

The value in the print is showing as b'.\x00\x00\x00\x00\x00\x00\x00'

Then when I try to convert the bytes into an integer

currentValue = int.from_bytes(cursor.fetchone()[0], byteorder="big")

I get an outrageously big number 3530822107858468864

Executing the exact same query in the SQL Server Studio I can see that the actual current_value is only 40. Am I doing a conversion wrong or is there something I'm not seeing?

NOTE

Just adding an extra mention that I'm using Python3 (in case anyone doesn't see the tag)

CodePudding user response:

Bigint is converted to float64, you can use CAST and VARCHAR and circumvent it

cursor = conn.cursor()
cursor.execute("select CAST(current_value AS VARCHAR(20)) AS current_value from sys.sequences where name = 'testSequence'")
currentValue = cursor.fetchone()[0]
cursor.close()
print(currentValue)
  • Related