0

SQL statements in Python continue to plague me. I'm trying to get the following expression written in a stand alone script:

enter image description here

This is what I've got so far but I don't think it's even close to the right syntax. I continue to get a syntax error at each iteration of this statement I try. Is there a guide to placing operands, field names, values, double and single quotes?

QF5 = "Week"
QF6 = "Wk"
WC4 = '" QF5" + ' <> "'" + QF6 + "'" AND '" QF6" + ' <> "''''"
  • @ziggy has a good answer below but here is a tip. One thing I do sometimes when I have complex SQL queries and I need a hint as to how they might look in python; I will set up the query in model builder or run the select tool then use the option to export to a python snippet. While the output is usually not the best code, it can give you clues as to how the query can be set up. – landocalrissian Oct 21 '16 at 19:26
2

you should always avoid using the + sign for concatenation. it gets really clunky as you can see. You should be using the .format method, it is cleaner

QF5 = "Week"
QF6 = "Wk"
WC4 = " '{}' <> '{}' AND '{}' <> '' ".format(QF5,QF6,QF6)

output = 'Week' <> 'Wk' AND 'Wk' <> ''

if the above doesnt work try this

QF5 = "Week"
QF6 = "Wk"
WC4 = """ '{}' <> '{}' AND '{}' <> '' """.format(QF5,QF6,QF6)

output ='Week' <> 'Wk' AND 'Wk' <> ''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.