• You can actually specify a length larger than the total string and substring will work fine, so the following will work too:

    declare @STR varchar(max), @strlen int, @startIndex int, @blockSize int

    select @STR = 'some long sql string',

    @strlen = len(@str), @startIndex = 1, @blockSize = 4000

    while(@startIndex <= @strlen)

    begin

    print substring(@str,@startIndex,@blockSize)

    set @startIndex = @startIndex + @blockSize

    end