Home > Mobile >  Running SQL in Python with Escaped Characters
Running SQL in Python with Escaped Characters

Time:09-11

I need to run the following code in SQL (Snowflake) from Python.

"WHERE SOMECOLUMN  LIKE 'product\\\\_%\\\\_season\\\\_s%\\\\_premium' ESCAPE '\\\\'"

For the avoidance of doubt each backslash in the SQL is repeated twice.

When I run this through Python I get the error

001003 (42000): SQL compilation error:
syntax error line 1 at position 358 unexpected '_'.

How can I properly escape this string?

CodePudding user response:

ESCAPE string could be any character, so using different character is easier and more readable, for instance ~:

escape

Character(s) inserted in front of a wildcard character to indicate that the wildcard should be interpreted as a regular character and not as a wildcard.

"WHERE SOMECOLUMN  LIKE 'product~_%~_season~_s%~_premium' ESCAPE '~'"

CodePudding user response:

It turns out I needed to escape the % with %% and the \\ with \\\\

  • Related