modified SearchToSQL function