tsql 从内容中获取电话号码


/* 
 * 从内容中获取电话号码
 */
--SELECT dbo.GeTelFromContent('已订18620023427/4')
ALTER function [dbo].[GeTelFromContent](@str VARCHAR(MAX))
RETURNS VARCHAR(50)
AS
BEGIN
	DECLARE @validchars VARCHAR(100) = '[1][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
	DECLARE @idx INT,@Result VARCHAR(50)
	
	SET @idx = PATINDEX('%'+ @validchars +'%',@str)
	--SELECT @idx
	IF @idx > 0 AND ( @idx = LEN(@str)-10 OR PATINDEX(SUBSTRING(@str,@idx+11,1),'[0-9]')=0 )
	BEGIN
		SET @Result=SUBSTRING(@str ,PATINDEX('%'+ @validchars +'%',@str), 11)
	END
	ELSE
	BEGIN
		SET @Result = ''
	END

	RETURN @Result

END