First, I confess I’m not realy experimented with regular expressions. I know how use it but when I want to build one, it’s something else… I’m going to document me.
I want to extract the WHERE clause in a SQL query. My goal is to be able to add an condition, like this:
SELECT * FROM myTbl WHERE columnA = 'B' AND columnB = 'C' ORDER BY columnX GROUP BY columnZ LIMIT 5
TO :
SELECT * FROM myTbl WHERE columnC = 'D' AND (columnA = 'B' AND columnB = 'C') ORDER BY columnX GROUP BY columnZ LIMIT 5
I tried some expression but I’m so void…
(where (.*)(?<=order by))
I wanted to get all between ‘where’ and (‘order by’ or ‘limit’ or ‘group by’)…
Anyone have an advice for me ? I have done some search and I don’t find anything like this. I found SQL Parser but these engines are too big compared to the task I want to complete.
Thank you.
Since the
WHEREclause can be quite complex (including subqueries which may include anORDER BYin some cases, for instance when used withFOR XML), so you will not be able to really find an always reliably working solution with a regex.A better solution would be to use a proper SQL parser which generates an AST, and then you can just extract the
WHEREclause from that. For T-SQL, you could use the parser from the bsn ModuleStore project (LGPL license). Modifying the AST is easy and you can re-script the statement afterwards.