CREATE FUNCTION dbo.Fnonlyint (@Text VARCHAR(8000))
/*****************************************************************************
Name: dbo.fnOnlyInt
Considers only the numbers.
******************************************************************************
Change History
******************************************************************************
Date: Author: Description:
----- ------- -------------------
09 Mar 2017 Leandro Buffone Created Initial Version
******************************************************************************
select dbo.fnOnlyInt('(0221-2345678)' )
*/
returns VARCHAR(8000)
BEGIN
DECLARE @Result VARCHAR(8000)
SET @Result = '';
WITH split
AS (SELECT 1 AS ID,
Substring(@Text, 1, 1) AS Word
UNION ALL
SELECT id + 1,
Substring(@Text, id + 1, 1)
FROM split
WHERE id < Len(@Text))
SELECT @Result += ( CASE
WHEN word LIKE '[0-9]' THEN word
ELSE ''
END )
FROM split
OPTION(maxrecursion 0)
RETURN @Result
END