--列车车次信息数据表
CREATE TABLE tb(col varchar(100))
INSERT tb SELECT '1434/1/2/14'
UNION ALL SELECT '"10653(85707)"'
UNION ALL SELECT '"32608/7(83212/1)"'
UNION ALL SELECT '"50057()"'
UNION ALL SELECT '"T888(备)"'
UNION ALL SELECT '"21058(81404/3)0"'
UNION ALL SELECT '"22028(80404.10264)"'
UNION ALL SELECT '20037(80303.84006/9)'
UNION ALL SELECT '24031(80410/9'
UNION ALL SELECT '24048(80904)(23118)'
UNION ALL SELECT '22080(80406.83080.10284)'
UNION ALL SELECT '0031(5632 5629. 1434/1/2/14)'
CREATE FUNCTION dbo.f_CompSTR(
@str varchar(8000), --包含车次的字符串
@find varchar(50) --要查询的值
)RETURNS bit
AS
BEGIN
IF @str=@find RETURN(1)
IF LEN(@str)<LEN(@find) RETURN(0)
--替换掉车次字符串中的无效数据
SELECT @str=REPLACE(@str,a,b)
FROM(--采用表集的处理方法,是为了方便以后扩充处理其他无效数据
SELECT a='"',b='')a
--统计数据分隔符
SELECT @str=REPLACE(@str,a,b)
FROM(
SELECT a='(',b='\' UNION ALL
SELECT a=')',b='\' UNION ALL
SELECT a='(',b='\' UNION ALL
SELECT a=')',b='\' UNION ALL
SELECT a=' ',b='\' UNION ALL
SELECT a=' ',b='\' UNION ALL
SELECT a='.',b='\' UNION ALL
SELECT a='.',b='\')a
--分拆比较处理
DECLARE @s1 varchar(8000),@h varchar(100),@s varchar(100),@l int
WHILE @str>''
BEGIN
SELECT @s1=LEFT(@str,CHARINDEX('\',@str+'\')-1),
@str=STUFF(@str,1,CHARINDEX('\',@str+'\'),''),
@h=LEFT(@s1,CHARINDEX('/',@s1+'/')-1),
@l=LEN(@h)+1
IF @h=@find RETURN(1)
WHILE CHARINDEX('/',@s1+'/')>0
BEGIN
SELECT @s=LEFT(@s1,CHARINDEX('/',@s1+'/')-1),
@s1=STUFF(@s1,1,CHARINDEX('/',@s1+'/'),'')
IF LEN(@s)<@l
SET @s=STUFF(@h,@l-LEN(@s),8000,@s)
IF @find=CASE
WHEN LEN(@s)<@l THEN STUFF(@h,@l-LEN(@s),8000,@s)
ELSE @S END
RETURN(1)
END
END
RETURN(0)
END
GO
--调用上述函数查询包含车次1434的记录
SELECT * FROM tb WHERE dbo. f_CompSTR(col,’143’)=1