Changed advance query so it can support previous versions of SQL Server and still compatible with Oracle.