Skip to content

How to extract all the conditions inside a where #3227

Closed Answered by georgesittas
thebruuu asked this question in Q&A
Discussion options

You must be logged in to vote

Perhaps something like this works, at least if your WHERE filters are in normal form (either CNF or DNF):

>>> import sqlglot
>>>
>>> ast = sqlglot.parse_one("select * from X where (A=1 or B=2) and C=3")
>>> for cond in ast.args["where"].this.flatten():
...     print(cond.sql())
...
A = 1 OR B = 2
C = 3

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@thebruuu
Comment options

Answer selected by tobymao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants