Search code examples
sql-serverpython-3.xpymssql

comparing an int variable to MSSQL query results Python via pymssql


I'm trying to compare a variable to query results in the following which are dates in yyyymmdd format. The database field LastUpdate is of type int.

When I test this, I think I am getting the false result because the query is a tuple compared to my variable as int.

Is there a way to convert the (20160422,) to an int?

import pymssql

conn = pymssql.connect(server='', user='', password='', database='')

cur = conn.cursor()
cur.execute('SELECT Top 1 LastUpdate FROM tbl_Date')

myDate = 20160422

for row in cur:
    print(row)

    if row == myDate:
        print('true')
        #then do something
    else:
        print('false')

conn.close()

Solution

  • As your suggestion, I've got this working based as follows; I just wasn't sure how to access elements of an array in Python, I just needed a clue. Thanks again.

    import pymssql
    
    conn = pymssql.connect(server='', user='', password='', database='')
    
    cur = conn.cursor()
    cur.execute('SELECT Top 1 LastUpdate FROM tbl_Date')
    
    myDate = 20160426
    
    for row in cur:
        print(row[0])
    
    if row[0] == myDate:
        print('true')
        #then do something
    else:
        print('false')
    
    conn.close()