Scalar-valued Functions [dbo].[ExtractNumericCharacters]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@inStringnvarchar(max)max
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
/*
* Extracts only the numbers from a string.
* @param @inString - A nvarchar(max) string to extract numbers from.
*/

create function [dbo].[ExtractNumericCharacters]
(
    @inString nvarchar(max)
)
returns nvarchar(max)
as
begin
    if( dbo.IsNullOrWhiteSpace( @inString ) = 1 )
    begin
        return null
    end
    declare @indexOfNumeric int
    set @indexOfNumeric = patindex('%[^0-9]%', @inString)
    begin
        while @indexOfNumeric > 0
        begin
            set @inString = stuff(@inString, @indexOfNumeric, 1, '' )
            set @indexOfNumeric = patindex('%[^0-9]%', @inString )
        end
    end
    return @inString
end
GO
GRANT EXECUTE ON  [dbo].[ExtractNumericCharacters] TO [MssExec]
GO
Uses
Used By