亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

mysql中取字符串中的數字的語句

標簽:
MySQL

           复制代码 代码如下:
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

上在的

select substring(所查询字符串,patindex('%[^0-9][0-9]%',所查询字符串)+1,patindex('%[0-9][^0-9]%',所查询字符串)-patindex('%[^0-9][0-9]%',所查询字符串)) 这个只能查询第一次在字符串出现的数字串

那么如果出现字符串什么样子的呢 sss8989sss //www.jb51.net ss8989ss8989ss8989 7879aafds789 432432432543534 应该怎么取呢


实例
复制代码 代码如下:
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


这样之后不管你是那种组合我们都可以方便的把字符中的数字全部取出来。                 

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
移動開發工程師
手記
粉絲
20
獲贊與收藏
132

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消