fixed sql to check for code