15.字符串处理案例--列车车次查询

--列车车次信息数据表
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
BEGINIF @str=@find RETURN(1)IF LEN(@str)''BEGINSELECT @s1=LEFT(@str,CHARINDEX('\',@str+'\')-1),@str=STUFF(@str,1,CHARINDEX('\',@str+'\'),''),@h=LEFT(@s1,CHARINDEX('/',@s1+'/')-1),@l=LEN(@h)+1IF @h=@find RETURN(1)WHILE CHARINDEX('/',@s1+'/')>0BEGINSELECT @s=LEFT(@s1,CHARINDEX('/',@s1+'/')-1),@s1=STUFF(@s1,1,CHARINDEX('/',@s1+'/'),'')IF LEN(@s)<@lSET @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 ENDRETURN(1)	ENDENDRETURN(0)
END
GO--调用上述函数查询包含车次1434的记录
SELECT * FROM tb WHERE dbo. f_CompSTR(col,’143’)=1

本文链接:https://my.lmcjl.com/post/16450.html

展开阅读全文

4 评论

留下您的评论.