MySQL中取字符串中的数字的语句
2022-11-12 09:24:31
内容摘要
这篇文章主要为大家详细介绍了MySQL中取字符串中的数字的语句,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
one:
declar
文章正文
这篇文章主要为大家详细介绍了MySQL中取字符串中的数字的语句,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <code> one: declare @s varchar(20) declare @i varchar(20) set @i= '' set @s= '新会员必须购买350元产品' while PATINDEX ( '%[0-9]%' , @s)>0 begin set @i=@i+substring(@s,PATINDEX ( '%[0-9]%' , @s),1) set @s=stuff(@s,1,PATINDEX ( '%[0-9]%' , @s), '' ) end select @i -- 300 two: declare @a table(id int identity(1,1),a varchar(100)) insert @a select '新会员必须购买350元产品' union all select '新店首次定货必须满20000元' select left(right(a,len(a)-patindex( '%[0-9]%' ,a)+1),len(right(a,len(a)-patindex( '%[0-9]%' ,a)+1))-1) from @a </code> |
代码如下:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <code> create function fn_GetNum(@s varchar(8000)) returns varchar(8000) as begin select @s = stuff(stuff(@s, 1, patindex( '%[0-9, .]%' , @s) - 1, '' ), patindex( '%[^0-9, .]%' , stuff(@s, 1, patindex( '%[0-9, .]%' , @s) - 1, '' )), len(@s), '' ) return @s end declare @t table(s varchar(8000)) insert @t select 'aaa11112bbb' union all select 'ccc212sss' union all select 'sss21a' select dbo.fn_GetNum(s) as result from @t select substring(s,patindex( '%[^0-9][0-9]%' ,s)+1,patindex( '%[0-9][^0-9]%' ,s)-patindex( '%[^0-9][0-9]%' ,s)) from @t /*功能:获取字符串中的字母*/ CREATE FUNCTION dbo.F_Get_STR (@S VARCHAR(100)) RETURNS VARCHAR(100) AS BEGIN WHILE PATINDEX( '%[^a-z]%' ,@S)>0 BEGIN set @s=stuff(@s,patindex( '%[^a-z]%' ,@s),1, '' ) END RETURN @S END GO --测试 select dbo.F_Get_STR( '测试ABC123ABC' ) GO /* 功能:获取字符串中的数字 */ create function dbo.F_Get_Number (@S varchar(100)) returns int AS begin while PATINDEX( '%[^0-9]%' ,@S)>0 begin set @s=stuff(@s,patindex( '%[^0-9]%' ,@s),1, '' ) end return cast(@S as int) end --测试 ---select dbo.F_Get_Number( '测试AB3C123AB5C' ) GO </code> |
注:关于MySQL中取字符串中的数字的语句的内容就先介绍到这里,更多相关文章的可以留意
代码注释