in reply to "if" to "case ... end"

Hmm, it might be possible with regexes if your SQL statements are always quite simple, but facing such an issue, I would probably go for real parsing.

This means you would have to write an SQL grammar (or possibly finding one on the Internet) and use a parser module such as, for example, Damian Conway's Parse::RecDescent (http://search.cpan.org/~jtbraun/Parse-RecDescent-1.967009/lib/Parse/RecDescent.pm).

Or, perhaps better (but I have not tried any of these), SQL parsing modules such as http://search.cpan.org/~team/Parse-SQL-0.002/lib/Parse/SQL.pm, http://search.cpan.org/~rehsack/SQL-Statement-1.405/lib/SQL/Parser.pm, etc., there are many of them on the CPAN, I do not know which would be really useful.