I cannot help you; if you don't understand the purpose of your program (i.e. the database query) then you have big problems.
However, some pointers:
- use the concat operator, i.e. instead of $sqlcode = "$sqlcode AND ( blah )"; say $sqlcode .= "AND ( blah )";
- reformat your SQL query as I have done to try and make the SQL easier to understand
- print out your query after constructing it (e.g. print( $sqlcode );)
- pay close attention to your errors - does Perl generate warnings? Does your database generate warnings?