pythonpypyodbc

Using % in like clause of select statement in python


import pypyodbc as pyodbc
model_name = 'test'
model_name1 = Master_Cursor.execute("select col1,col2,col3 from tablename where col3 like '%s' order by col3" %(model_name)).fetchall()

above code returns one record matching model_name = test. How would I get back other records having model_name=123test123,abctestabc,ABCtestABC etc??

Basically, looking for

select col1,col2,col3 from tablename where col3 like '%test%'.

Solution

  • model_name1 = Master_Cursor.execute("select col1,col2,col3 from tablename where col3 like '%%%s%%' order by col3" %(model_name)).fetchall()
    

    Use 2 percents to represent a real percent character