sql获取数字:只是将字符串中所有的数字组织在一起,比如“test12tt456”,返回的是12456
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 Go
若是想从“test12tt456”取到12 和456两个数字,则为:
declare @str varchar(100),@tmp varchar(101),@i int set @str='sddfd123fddfd56fddf78' set @tmp=@str+'a' while patindex('%[0-9]%',@tmp)>0 begin set @i=1 while 1=1 begin if isnumeric(substring(@tmp,patindex('%[0-9]%',@tmp)+@i,1))=0 break set @i=@i+1 end print substring(@tmp,patindex('%[0-9]%',@tmp),@i) set @tmp=stuff(@tmp,patindex('%[0-9]%',@tmp),@i,'') end
/*
123
56
78
*/
sql获取非数字:
create function dbo.F_Get_NANnumber (@S varchar(100)) returns varchar(500) AS begin while PATINDEX('%[0-9]%',@S)>0 begin set @s=stuff(@s,patindex('%[0-9]%',@s),1,'') end return @s end GO