Error converting data type varchar to numeric
It's because the first parameter of the REPLICATE function is a VARCHAR, not int. Please see the documentation here.
Instead of doing REPLICATE(0, 4) you'll want to do REPLICATE('0', 4) etc.
The error itself can be a little confusing when it says the word convert, because it doesn't necessarily mean the CONVERT function, rather it's trying to do implicit conversion of the INT value 0 to a VARCHAR value since that is the data type of the parameter for the REPLICATE function. This error can occur for any function that expects a certain data type for its parameter but is passed in a value that's a different data type that is unable to be implicitly converted.
An example of implicit conversion that would work, is if a function had a parameter that was of type BIGINT and you passed in an INT value like 13 instead.