in reply to Substitutions Within Substitutions

And somewhat sidestepping the original problem, you might try and use something like SQL::Statement to parse your data (specifically probably the perldoc for "SQL::Statement::Structure" is of interest; see link in the aforementioned module's docs) and then manipulate that form and re-output the modified SQL after you've twiddled it accordingly.

Update: Grrr, link to ...::Structure wasn't working. Reworded.