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)')

excelguy
  • 1,574
  • 6
  • 33
  • 67

2 Answers2

2

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
Gro
  • 1,613
  • 1
  • 13
  • 19
1

Just make it a raw string:

businessDate = r"'2019-03-13'"
  • Thanks anton, what if I wanted to use `businessDate` in my function as an argument. Ie. `def stack(businessDate):` – excelguy Apr 08 '19 at 20:36
  • 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) – Anton Rasmussen Apr 08 '19 at 20:40
  • 1
    Sorry Anton im having trouble following, can I message you? – excelguy Apr 08 '19 at 20:49