Cannot call methods on nvarchar(max) @KeyField.UPPER().REPLACE('CODE','').REPLACE('ID','') @KeyField.REPLACE or 'HELLOWORLD'.REPLACE will throw the same error The correct way to call it is REPLACE(REPLACE(UPPER(@KeyField),'CODE',''),'ID','')