SQL search with logical operators SQL search with logical operators sql-server sql-server

SQL search with logical operators


The parsing of the query via and/or/() etc needs to be done via some kind of parser. For something this simple, a shunting-yard algorithm should do nicely (and is how we handle the and/or/not etc when filtering SE on tags, although the full support for bracketed sub-expressions is only available internally). This will generate a tree of the operations, i.e. (here using prefix representation, but it is more typically used to produce an AST)

and("Douglas", or("adam", "adams"))

which you then need to use to generate TSQL. Not trivial, but not rocket-science either.