--列车车次信息数据表 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)'' 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
15.字符串处理案例--列车车次查询
最新推荐文章于 2024-11-03 10:21:05 发布