Created dynamic sql for search function