Thx Jan. But the thing is after the app restarts, the error goes away and the macro resolves well. When the error happens, the null exception is not only on {%ToInt(tagid, "")%} but is also on {%tagname%} and {%ToInt(groupid, "")%}. See the whole where clause applied on the repeater:
({%ToInt(tagid, "")%} = 0 AND '{%tagname%}'='') OR (DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = {%ToInt(tagid, "")%})) OR (DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID IN (SELECT TagID FROM CMS_Tag WHERE TagName = '{%tagname%}' AND TagGroupID = {%ToInt(groupid, "")%})))