語 MS SQL .. '1001' , '0101' 企 襦 4襴 覓語企 2讌 覓語襯... OR 觜 一一 伎 襦 4襴 覓語伎 襷れ 伎狩. 襯 れ 覓語 '1001' 螻 '0101' 襯 OR 觜 一壱.. '1101' 企朱 覓語襯 觸伎 蟇一.. 貎朱Μ襯 覈詞蟆伎.. 覿襴暑. |
/* drop table #temp create table #temp(seq int, a varchar(30), b varchar(30)) insert #temp values(1, '1001' , '0101') insert #temp values(2, '10011' , '01011') */ with dumy(num) as ( select 0 num union all select num + 1 from dumy where num + 1 <= 30 ), rs (seq, a, b, rs) as ( select a.seq , a.a , a.b , b.rs from #temp a cross apply ( select case when '1' in (substring(a.a, num+1, 1), substring(a.b, num+1, 1)) then '1' else '0' end rs from dumy where num <= case when len(a.a) > len(a.b) then len(a.a) else len(a.b) end - 1 ) b ) select distinct seq , a , b , (select cast(rs as varchar(100)) as 'text()' from rs where a.seq = seq for xml path('')) [a | b] from rs a
2讌 | 10讌 |
1101 | 13 |
0101 | 5 |
select 13|5
--drop table #temp create table #temp(id int, col1 varchar(30), col2 varchar(30)) insert #temp values(1, '1001' , '0101') insert #temp values(2, '10011' , '01011') ;with dummy(seq) as ( select 1 seq union all select seq + 1 from dummy where seq + 1 <= 30 ), result as ( select a.id , a.col1 , a.col2 , sign(convert(tinyint, substring(col1, seq, 1)) + substring(col2, seq, 1)) rs from #temp a cross join dummy b where b.seq <= len(a.col1) ) select id , x [col1 | col2] from #temp a cross apply ( select ( select cast(rs as varchar(100)) as 'text()' from result where a.id = id for xml path('')) x ) b