1.提取字符串中的数字 人民路1号501室 结果1-501
sql函数如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_GetNumeric]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_GetNumeric]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION [dbo].[f_GetNumeric]
(
@strValue as varchar(8000)
)
RETURNS varchar(8000) AS
BEGIN
Declare @Result as varchar(8000),
@Temp as varchar(4)
--变量初始
Set @Result=''
While Len(@strValue)>0
Begin
Set @Temp=Left(@strValue,1)
If Isnumeric(@Temp)=1
Begin
Set @Result=@Result+@Temp
End
Else
If Isnull(@Result,'')<>'' Set @Result=@Result+'-'
If Len(@strValue)=1
Set @strValue=''
Else
Set @strValue=Right(@strValue,Len(@strValue)-1)
End
If Isnull(@Result,'')<>''
If Right(@Result,1)='-' Set @Result=Left(@Result,Len(@Result)-1)
Return @Result
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO