• his part works but everytime I look at all the IFs I get confused so I thought there might be a better way to achive the same result.

    Having before and after test data to work with (plus the English text to explain the transformations needed) also helps in these cases as I see you got no help after you posted the entire stored procedure. You may also have to explain the context, as in why you are using dynamic sql and not protecting against SQL injection in your code.

    ----------------------------------------------------