0

Question is similar to Pythonic way to create a long multi-line string

However, I need to insert a variable wrapped in single quotes into my query. Cant seem to get it to work. Whenver I output my query I am just getting 2019-03-13 and I think it should be '2019-03-13'

businessDate = '2019-03-13'


    sql = f"""   
#query goes in here.
 and businessdate = {businessDate}

    """

error msg:

': ('42883', '[42883] ERROR 4286: Operator does not exist: date = int\nHINT: No operator matches the given name and argument type(s). You may need to add explicit type casts\n (4286) (SQLExecDirectW)')

3 Answers 3

3

Pythonic way of achieving String interpolation is

businessDate = '2019-03-13'
sql = """   
 #query goes in here.
 and businessdate = '%s'
""" %businessDate

print(sql) #to check the end result
Sign up to request clarification or add additional context in comments.

Comments

1

Just make it a raw string:

businessDate = r"'2019-03-13'"

3 Comments

Thanks anton, what if I wanted to use businessDate in my function as an argument. Ie. def stack(businessDate):
Shouldn't make a difference, as long as you use the r" 'blah' " syntax: Just play around in the shell: >>> biz_date=r"'1000'" >>> print(biz_date) '1000' >>> def blah(blah): ... print(blah) ... >>> blah(biz_date) '1000' Note that print casts to str.... so, depending on your functionality, you may have to explicitly cast it like str(blah)
Sorry Anton im having trouble following, can I message you?
0

Try this below code

query = """
    SELECT *
    FROM insider_trading
    WHERE (column1|| column1) ILIKE %s
    ORDER BY accepted_datetime
"""
cursor.execute(query, ('%' + search_query + '%',))
rows = cursor.fetchall()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.