Hey, thanks to the above answer I changed my code to the following:
('{%ToInt(QueryString.categoryid, "")#%}' = 0 AND '{%categoryname%}'='') OR (DocumentID IN (SELECT DocumentID FROM CMS_DocumentCategory WHERE CategoryID = {% ToInt(QueryString.categoryid) #%}))
I had to shorten it but the correct answer worked fine for me.