Skip to content

Instantly share code, notes, and snippets.

@PRElias
Created April 11, 2019 16:04
Show Gist options
  • Save PRElias/7b9fbb290ee44fbf809260d9fb7afac5 to your computer and use it in GitHub Desktop.
Save PRElias/7b9fbb290ee44fbf809260d9fb7afac5 to your computer and use it in GitHub Desktop.
SQL Server GetNumeric function
/****** Object: UserDefinedFunction [dbo].[GetNumeric] Script Date: 11/04/2019 11:51:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[GetNumeric]
(@strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE @intAlpha > 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN ISNULL(@strAlphaNumeric,0)
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment