From
Here
Prajesh says "
a function for you in TSQL doing exactly what you want. It takes 13 secs if you dont use the function for 100,000 records and 8 secs if you are using functions for same records, so it seems pretty fast."
SELECT Data
FROM TestFunctionPerf
SELECT dbo.fnGetNumericOnly(Data) AS Data
FROM TestFunctionPerf
CREATE FUNCTION fnGetNumericOnly (@string VARCHAR(500))
RETURNS VARCHAR(500)
AS
BEGIN
DECLARE @NumericOnlyPart VARCHAR(500) = '';
DECLARE @Numeric VARCHAR(1) = ''
DECLARE @start INT = 1;
DECLARE @end INT = 1
SELECT @end = LEN(@string);
WHILE (@start <= @end)
BEGIN
SET @Numeric = SUBSTRING(@string, @start, @start + 1)
IF ASCII(@Numeric) >= 48
AND ASCII(@Numeric) <= 57
BEGIN
SET @NumericOnlyPart = @NumericOnlyPart + @Numeric;
END
SET @start = @start + 1;
END
RETURN @NumericOnlyPart
END
GO
SELECT dbo.fnGetNumericOnly('12 fgfgf.,jhkjh khk 56789jhjh67')
GO
CREATE TABLE TestFunctionPerf (Data VARCHAR(8000))
GO
TRUNCATE TABLE TestFunctionPerf
GO
DECLARE @start INT = 1;
DECLARE @end INT = 100000
WHILE (@start <= @end)
BEGIN
INSERT INTO TestFunctionPerf
VALUES ('12 fgfgf.,jhkjh khk 56789jhjh67')
SET @start = @start + 1
END
GO
SELECT Data
FROM TestFunctionPerf
SELECT dbo.fnGetNumericOnly(Data) AS Data
FROM TestFunctionPerf
Comments
Post a Comment