0

QUESTION: Same Error as this ValueError: unsupported format character ']' (0x5d) but I can't seem to find any %

I am running a query in Python env using cur.execute()

with conn.cursor() as cur:
    cur.execute(f"""
   SELECT 
    FirstName,
    Surname,
   FORMAT(TotalHours / NULLIF((ContractHours - RemissionHours), 0) * 100, 'N2') AS [Utilisation %]

   FROM t
   JOIN Staff ON Staff.SID = t.SID

   WHERE (@IncludeLeavers = 'Yes' OR @IncludeLeavers= 'No' AND Staff.IsCurrent = 1)
   
   """,
        params={
          "IncludeLeavers": REPORT_OPTIONS["IncludeLeavers"][0]
        })

Error:

ValueError : unsupported format character ']' (0x5d) at index 511[' File "<'string'>",

on this line IncludeLeavers": REPORT_OPTIONS["IncludeLeavers"][0]

To provide values to/ initialise the variable @IncludeLeavers, I have initialised it in Options, which need to match the query results.

OPTIONS

4
  • Could you provide some more context? What function are you trying to run? Some type of sql command? I know '[' and ']' are reserved characters in sql Commented Aug 30, 2022 at 14:49
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Aug 30, 2022 at 15:34
  • @PatrickGorman Yes an sql query using pytds.connect & cur.execute(). The params is the 2nd part of the execute func. The query is finished before the start of the params definition so the reserved chars should still work, they do in other similar reports. Commented Aug 31, 2022 at 15:40
  • 2
    The issue is the percent character and %] sequence. You'll need to escape the percent. See duplicate question stackoverflow.com/questions/12586544/… and thewebdev.info/2022/04/15/… Commented Aug 31, 2022 at 15:50

1 Answer 1

0

As said by @Sarah,

The issue is the percent character and %] sequence.

Adding another % wherever % was used, fixed the issue. Thanks Sarah.

Sign up to request clarification or add additional context in comments.

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.