Search code examples
sqlsql-serversubstring

Substringing Between 2 Words at End of Query


I am currently navigating a query table that contains a query_id field and a query_text field. In the query_text field, there are a variety of levels of detail that have been added. For example, a query may be as simple as what’s shown for ID.1, or as complex as ID.2:

--ID.1

Select *
From Custtable;

--ID.2 

Select 
    Custtable.CustId
    , locsub.StateCode
From Custtable
Inner join (
    Select
        Loctable.CustId
        , Loctable.CountryCode
        , Loctable.StateCode
    From Loctable
    Where Loctable.CountryCode = 'US'
    ) locsub
On Custtable.CustId = Locsub.CustId
Where Locsub.StateCode= 'NY'
And left(Custtable.CustId, 5) = 'FOOD-';

What I need to do is be able to reliably select the table alias and join field in the last where clause of something like ID.2. In this example, I would need to be able to select just Locsub.StateCode, not the = ‘NY’, to get a better sense of the relationships in the dataset. At the moment, I am able to select everything after the ‘WHERE’, but am unable to get only the information right before the ‘=’. My assumption is I need a way of indexing the = right after the where, and selecting before that, but I can't figure out how...

I am currently doing the following:

Select
    substring(
        query_text, 
        len(query_text) - charindex('erehw', reverse(query_text))
        , charindex('=', reverse(query_text))
        ) 
From mytable

This works, but again, it is giving me all information after the where statement, instead of just the join field. Any tips would be appreciated!


Solution

    1. How the heck did you get into this mess?
    2. This cannot reliably be done with just SQL. I could show you how to wrap what you have in ANOTHER reversing and finding the AND, but that's fools gold -- that AND could be an OR, or the first bit could be (1=1) and THEN the real query could be happening, or there could be a subquery.
    3. Oh, new idea, why not just split things up by spaces and look for a TABLE.COLUMN pattern? Sure, but what if they don't specify the table?
    4. SQL is a full-blown programming language. This is not a task that you should be given or have taken on. If I were in your shoes, and I HAD to do this, well, here's the bad news: you'll probably have to use regular expressions. You can Google around for how to get that going on SQL Server and Regexes on getting started on chopping up SQL statements properly.
    5. This is never going to work in all cases, let alone be resistant to people actively trying to hide the table/field they are querying.