你很亲密。也许这会有所帮助
-- Just a Demonstrative Table Variable.
Declare @YourTable Table ([DepartmentNo] varchar(50)) Insert Into @YourTable Values
('403,273,828')
,('403,273,829')
,('403,273,830')
,('403,273,831')
,('634')
,('634')
,('7')
-- The Actual Query
Select *
from @YourTable A
cross apply string_split([DepartmentNo],',') B
Where B.value in (select * from string_split('634,7,830',','))
结果
DepartmentNo value
403,273,830 830
634 634
634 634
7 7
这是一个剥离控制字符的 SVF
CREATE FUNCTION [dbo].[svf-Str-Strip-Control](@S varchar(max))
Returns varchar(max)
Begin
Select @S=Replace(@S,char(n),' ')
From (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31) ) N(n)
Return ltrim(rtrim(replace(replace(replace(@S,' ','†‡'),'‡†',''),'†‡',' ')))
End
--Select [dbo].[svf-Str-Strip-Control]('Michael '+char(13)+char(10)+'LastName') --Returns: Michael LastName
然后更新答案
Declare @YourTable Table ([DepartmentNo] varchar(50)) Insert Into @YourTable Values
('403,273,828')
,('403,273,829')
,('403,273,830'+char(8)) -- Notice char(8)
,('403,273,831')
,('634')
,('634')
,('7')
Select *
from @YourTable A
cross apply string_split([dbo].[svf-Str-Strip-Control]([DepartmentNo]),',') B
Where B.value in (select * from string_split('634,7,830',','))
结果同上或dbFiddle