2024-04-23 04:49:54
2024-04-23 05:01:18
2024-04-23 00:43:24
2024-04-23 00:17:38
IF OBJECT_ID('fn_GetNumberFromString') IS NOT NULL
BEGIN
DROP FUNCTION fn_GetNumberFromString;
END;
GO
CREATE FUNCTION fn_GetNumberFromString ( @str NVARCHAR(80) )
RETURNS INT
AS
BEGIN
DECLARE @i INT ,
@len INT;
SET @len = LEN(@str);
IF @len = 0
RETURN 0;
SET @i = 1;
DECLARE @char VARCHAR(1);
SET @char = LEFT(@str, 1);
DECLARE @outStr VARCHAR(80);--用于输出
DECLARE @tmpStr VARCHAR(81);--用于提高性能
SET @outStr = '';
SET @tmpStr = '0';
WHILE @i <= @len
BEGIN
IF PATINDEX('%[0-9]%', @char) = 1
AND PATINDEX('%[0-9]%', RIGHT(@tmpStr, 1)) = 1
BEGIN
SET @outStr += @char;
SET @tmpStr += @char;
END;
IF LEN(@outStr) > 0
AND PATINDEX('%[0-9]%', @char) = 0
SET @tmpStr += @char;
IF PATINDEX('%[0-9]%', RIGHT(@tmpStr, 1)) = 0
BEGIN
GOTO outLocation;
END;
SET @i += 1;
SET @char = SUBSTRING(@str, @i, 1);
END;
outLocation:
RETURN CASE WHEN @outStr='' THEN 0 ELSE CAST(@outStr AS INT ) END;
END;
GO
2024-04-23 02:29:23