0

I have the following Impala query called from Python, working fine.

reference_ts = "2017-03-11 00:00:00"

q = """

select col_A, col_B, trunc(my_timestamp, 'D') as arrival_wk
  from my_table
    where my_timestamp < "%s"

""" % (reference_ts)

my_df = my_conn.exec_query(q)

Then if I modify the query a little bit like below:

q1 = """

    select col_A, col_B, trunc(my_timestamp, 'D') as arrival_wk
      from my_table
        where my_timestamp < "%s"
        and col_C like 'AB%'

""" % (reference_ts)

my_df1 = my_conn.exec_query(q1)

I got the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-81-be1bb3b139a5> in <module>()
     15 
---> 16 """ % (reference_ts)
     17 

ValueError: unsupported format character ''' (0x27) at index 303

I am guessing Python is confused with the % in col_C like 'AB%' . I am wondering if there is a way to resolve this problem? Thanks!

Edamame
  • 23,718
  • 73
  • 186
  • 320
  • What does it do when you use 'AB%%' instead? – spijs Mar 30 '17 at 14:25
  • You could also try using `.format() ` – spijs Mar 30 '17 at 14:28
  • How do I use .format? do u have an example? Thanks! – Edamame Mar 30 '17 at 17:21
  • `"This is a string %s example containing %s substrings" % (s1,s2)` would have the same effect as `"This is a string {0} example containing {1} substrings".format(s1,s2)`. More info [here](http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) – spijs Mar 31 '17 at 06:34

0 Answers0