troubleshooting Question

Need a Regex to extract the correct FROM clause

Avatar of Russ Suter
Russ Suter asked on
.NET ProgrammingC#Regular Expressions
4 Comments1 Solution60 ViewsLast Modified:
I'm looking for a Regex that will allow me to select the appropriate FROM clause in a SQL SELECT statement. I'm looking for the FROM clause for the outermost SELECT statement only. The example below shows in bold what I mean. This query, although fairly crappy as a SQL query, fairly well demonstrates what I mean. You can see that there are 3 separate FROM clauses. I can't always rely upon it being first because of the inline query in the SELECT clause. I can't always rely on it being last because of the subquery in the WHERE clause. The one thing I can know for certain is that there will be a 1 to 1 relationship between the SELECT and FROM keywords. I need my Regex to EXACTLY and ONLY match the FROM keyword associated with the outermost query.

SELECT
      [InvoiceNumber],
      [Company],
      [DocumentType],
      [AccountNumber],
      [DateInvoiced],
      [DateDue],
      (SELECT SUM([Amount]) FROM [Invoices])
FROM
      [Invoices]
WHERE
      [InvoiceNumber] NOT IN
      (
      SELECT
            [InvoiceNumber]
      FROM
            [InvoiceLines]
      WHERE
            [TimeStamp] BETWEEN '1/1/2019' AND '12/31/2019'
      )
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros