我知道这是旧的,但我没有发布太多,如果有的话,我想我会在这里尝试。
如果最后一个数字始终是字符,为什么不使用带有 PATINDEX 测试的子字符串函数呢?我确定我的代码效率极低,但这是我的初学者尝试。我敢打赌,有些人会把我的案例陈述打得更小。
-- I used the code below
-- It only works if the final digit is lower or uppercase letter of
-- specified language.
DECLARE @CodeDIS TABLE (IdNumber nvarchar(50), BaseRowNum nvarchar(50));
insert into @CodeDIS (IdNumber,BaseRowNum) values ('52352345','1')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('134131','2')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1343141','3')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('a143321','4')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('c34324','5')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1343214','6')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('%134324','7')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('a%134324','8')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1413','9')
;
DECLARE @ListOfProducts TABLE (IdNumber nvarchar(50), UpdateSourceDataColumn nvarchar(50));
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('52352345a','11')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('134131','22')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343141a','33')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a143321','44')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('c343245','55')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343214Z','66')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('%134324','77')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a%134324','88')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1413\','99')
;
SELECT a.*,b.IdNumber as IdNumber2, b.UpdateSourceDataColumn
FROM @CodeDIS a
left join @ListOfProducts b
on a.IdNumber =
case
when PATINDEX(
'[a-zA-Z]',
substring(b.IdNumber,len(b.IdNumber),1)
COLLATE Latin1_General_CS_AS
)=1
then left(b.IdNumber,len(b.IdNumber)-1)
else b.IdNumber
end
;
--IdNumber BaseRowNum IdNumber2 UpdateSourceDataColumn
--52352345 1 52352345a 11
--134131 2 134131 22
--1343141 3 1343141a 33
--a143321 4 a143321 44
--c34324 5 NULL NULL
--1343214 6 1343214Z 66
--%134324 7 %134324 77
--a%134324 8 a%134324 88
--1413 9 NULL NULL