• I don't think there is any restriction on the # of characters REPLACE will work with as there isn't anything mentioned in BOL.

    My best guess is that the spaces you see are not the SPACE character, but may be other white space characters, like tab, and the code I posted won't replace a tab character. So if I have this:

    DECLARE @data NVARCHAR(MAX) = 'EXEC Proc_gen' + CHAR(9) + '@PsID=12, @kID=-1, @ID=3183';

    SELECT @data AS theData ,

    REPLACE(@data, ' ', '') AS dataNoSpaces;

    The CHAR(9), tab, is not replaced thus leaving white space between "proc_gen @PsID", while replacing the actual spaces.

    Also here's an example that shows that length is not an issue:

    DECLARE @data NVARCHAR(MAX) = REPLICATE(CONVERT(NVARCHAR(MAX),'EXEC Proc_gen @PsID=12, @kID=-1, @ID=3183'), 10000);

    SELECT

    @data AS theData ,

    REPLACE(@data, ' ', '') AS dataNoSpaces,

    LEN(@data) AS stringLength,

    DATALENGTH(@data);