我有一个如下表
如果相同位置的任何字符相等,我想获取表的结果
示例:
全名: ANURADHA KONGARI
名称: AK
没有缩写: AI
在此示例中,AK的首字母和AI的首字母相等。因此它应该返回结果。
我尝试使用substring
,但是会得到所有记录。由于长度
我尝试过的例子
select fullname, names, withoutinitials from #localtable where substring(names,1,1)= substring(withoutinitials,1,1)
or
substring(names,2,1)= substring(withoutinitials,2,1)
or
substring(names,3,1)= substring(withoutinitials,3,1)
这是我尝试的一种方式。它可以正常工作,但是如果字符串长度大于4怎么办?
create table #lt2(fullname varchar(100))
insert into #lt2 select getName=case
when len(names)=1 and substring(names,1,1) = substring(withoutinitials,1,1)
then fullname
when len(names)=2 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=3 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1)))
then fullname
when len(names)=4 and ((substring(names,1,1) = substring(withoutinitials,1,1))or(substring(names,2,1) = substring(withoutinitials,2,1)) or (substring(names,2,1) = substring(withoutinitials,2,1))
or (substring(names,3,1) = substring(withoutinitials,3,1)))
then fullname else ''
end
from #localtable
select * from #lt2 where fullname!=''
但这将导致所有记录,因为某些记录的长度可能不为3,所以它甚至返回长度为1的名称。
如果两个字符串中的至少一个字符在相同位置相等,我想得到结果。
像字符串1的第一位置=字符串2或2的第一位置 字符串1的位置=字符串2的第二个位置。
谢谢。
答案 0 :(得分:0)
您需要在位置上使用1和2,并且在没有两个字符的情况下还要添加一个空白检查。
declare @table table (fullname varchar(64), names varchar(2), withoutinititals varchar(2))
insert into @table
values
('GANGA RAJAM','GR','AM'),
('ANURADHA KONGARI','AK','AI'),
('PATEL SHIVAJI','R','H'),
('NEW NAME','X','X')
select
*
,substring(names,1,1)
,substring(names,2,1)
,substring(withoutinititals,1,1)
,substring(withoutinititals,2,1)
from @table
where
(substring(names,1,1) = substring(withoutinititals,1,1))
or
(substring(names,2,1) = substring(withoutinititals,2,1) and substring(withoutinititals,2,1) != '')
请注意,如果您删除where子句substring(withoutinititals,2,1) != ''
的这一部分,则会得到误报
答案 1 :(得分:0)
'0'
和names
的{{1}}字符不相等的右键盘:
'1'
带有示例数据:
withoutinitials
结果是:
select fullname, names, withoutinitials from #localtable where
substring(names, 1, 1) = substring(withoutinitials, 1, 1)
or
substring(LEFT(CONCAT(names, '0'), 2), 2, 1) = substring(LEFT(CONCAT(withoutinitials, '1'), 2), 2, 1)
or
substring(LEFT(CONCAT(names, '00'), 3), 3, 1) = substring(LEFT(CONCAT(withoutinitials, '11'), 3), 3, 1)
or
substring(LEFT(CONCAT(names, '000'), 4), 4, 1) = substring(LEFT(CONCAT(withoutinitials, '111'), 4), 4, 1)
答案 2 :(得分:0)
有点有趣,但是我尝试了递归CTE
--select distinct top 4000 employeeid, surname, ForeName1 forename into #test from isEmployeeMaster where Sequence = 1
;WITH S AS (SELECT 1 AS A, M.employeeid as E, M.Surname as sur, SUBSTRING(M.surname,1,1) ATOM FROM #test M
UNION ALL
SELECT A + 1, E, sur, SUBSTRING(S.sur,A+1,1) FROM S WHERE A < LEN(S.sur)
),
F AS (SELECT 1 AS A, M2.employeeid as E, M2.forename as fore, SUBSTRING(M2.forename,1,1) ATOM FROM #test M2
UNION ALL
SELECT A + 1, E, fore, SUBSTRING(f.fore,A+1,1) FROM f WHERE A < LEN(f.fore)
)
select t.* from #test t where t.EmployeeId in
(select s.e from S join F on S.E = F.E and S.Atom = F.Atom and S.A = F.a)
--drop table #test