with dumy as ( select 1 seq union all select seq + 1 from dumy where seq + 1 <= datediff(dd, '20060601', getdate()) ), rs as ( select seq , convert(char(6),dateadd(mm, seq, '20060601'), 112) yyyymm from dumy ), bits as ( select '111000101111000000' bit1, '111100101111000001' bit2 --232384 ^ 248769 ) select b.yyyymm from bits a inner join rs b on b.seq <= len(a.bit1) where substring(a.bit1, seq, 1) <> substring(a.bit2, seq, 1) option (maxrecursion 0);