SQL query rewriting is a core technique used in applications such as optimization, dialect translation, and testing, yet most existing tools remain restricted by dialect-specific parsers. We present SQLFlex, a novel framework that combines grammar-based parsing with LLM-powered segmentation to enable dialect-agnostic query rewriting. Our experiments across ten SQL dialects show that SQLFlex achieves higher correctness and broader applicability than prior tools in tasks including linting, test case reduction, and mutation testing.