• Shouldn't be too hard using Linq...

    using System.Linq;

    [Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "clrNGLoop_FillRow", TableDefinition = "Position int, Token nvarchar(4000)")]

    public static IEnumerable clrNGLoop(SqlString value, SqlInt32 n)

    {

    int idx = 1;

    Lookup<int, char> l = (Lookup < int, char> )value.Value.ToLookup(x => idx++, y => y);

    return l.Select(x => new Item() { Position = x.Key, Token = new string(l.Where(y => y.Key >= x.Key && y.Key < (x.Key + n.Value)).Select(z => z.First()).ToArray()) }).Where(v=>v.Token.Length == n.Value);

    }

    public static void clrNGLoop_FillRow(Object obj, out SqlInt32 position, out SqlString token)

    {

    Item item = (Item)obj;

    position = new SqlInt32(item.Position);

    token = new SqlString(item.Token);

    }

    public class Item

    {

    public int Position;

    public string Token;

    }