Fixed some bugs in SQL search querys