• Thanks! I found this function really useful. Just adding the middle line here got around the looping issue for me:

    set @pos = @ret

    if @pos = 0 set @counter = @Occurrence

    end