1

我正在使用访问 97。

这就是我想做的:

SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber LIKE ListOfProducts.IDNumber*;

有时 ListOfProducts 上的 IDNumber 有一个额外的字母,表示有关产品的一些信息。

所以我在 CodeDIS 上有 IDNumber AC244,但在 ListOfProducts 上有 AC244P。

我想要的只是在条件的末尾添加一个通配符。

这可能吗?

4

3 回答 3

1

如果选择是 C 或 P,为什么不:

SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber = ListOfProducts.IDNumber & "C"
OR CodeDIS.IDNumber = ListOfProducts.IDNumber & "P"
于 2012-08-29T19:25:41.357 回答
1

怎么样:

SELECT * 
FROM CodeDIS, ListOfProducts 
WHERE CodeDIS.IDNumber LIKE left(ListOfProducts.IDNumber,5); 

或者如果你喜欢:

SELECT * 
FROM CodeDIS a inner join ListOfProducts b on a.IDNumber = left(b.IDNumber,5); 
于 2012-08-29T19:34:35.837 回答
1

我知道这是旧的,但我没有发布太多,如果有的话,我想我会在这里尝试。

如果最后一个数字始终是字符,为什么不使用带有 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
于 2016-09-16T21:26:02.443 回答