字符串中提取数字

Posted on 2007-11-30 16:51 codeguy 阅读(1314) 评论(0)  编辑  收藏 所属分类: DataBase
在短信业务开发中,要求用户输入指令G+生日,业务逻辑通过提取不同的年份来下发相应的时运信息给用户。由于在实际业务运行时,用户上行的随意性,我们业务端需提取年份,经过反复修改,得出以下代码可基本满足业务需求:
declare @str varchar(200)
set @str 
= '我的生日是1963年5月6号'

if patIndex('%[^0-9]%', subString(@str, patIndex('%[0-9]%', @str), 4)) = 0
        and cast(subString(@str, patIndex(
'%[0-9]%', @str), 4) as int> 1900
        and cast(subString(@str, patIndex(
'%[0-9]%', @str), 4) as int< 2000
    print 
'The command is correct, and the number is: ' + subString(@str, patIndex('%[0-9]%', @str), 4)
else
    print 
'command error!!'
patIndex()函数可进行一定程度的模式匹配,弥补了t-sql中无正则表达式的缺陷,是一个很有用的函数,希望大家能重视起来,并加以应用。。

进入Java行业了,这一直都是我为之努力的。虽然目前很平庸,但我对自己有信心,长期的积累,总会成功。。

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 0, comments - 0, trackbacks - 0, articles - 3

Copyright © codeguy