Quantcast
Channel: Eric Niemiec's Blog » SQL Server
Viewing all articles
Browse latest Browse all 10

How to create IsAlphaNumeric, IsAlpha, IsNumeric functions or use RegEx expressions in SQL Server

$
0
0

There are times in T-SQL when you need to check a string to determine if it contains any special characters or numbers.  Using some of the built-in RegEx style operands in SQL Server, you can make these determinations without creating looping functions or external CLR user-defined functions.

While the built-in support is fairly limited, it is useful for several things as seen in the functions below.  You can create these functions using either the LIKE operator or PATINDEX.  The link to the LIKE operator shows a list of wildcard operands that you can use to build these functions.  While I have created functions that perform specific actions, you could create a generic function and pass in your expression to be evaluated or use the PATINDEX operator directly in your procedure.

While I have not been able to see a performance difference between the LIKE and PATINDEX operators on the size database that I maintain, you might be able to make a better judgment call on which one to use when running against larger result sets.  I personally have changed from using the LIKE syntax to the PATINDEX syntax simply out of preference.

http://msdn.microsoft.com/en-us/library/ms179859.aspx (LIKE documentation with wildcard patterns)

http://msdn.microsoft.com/en-us/library/ms188395.aspx (PATINDEX documentation)

At some point you will out-grow the built-in RegEx style support and will need to reach out to CLR functions, here are a few links to projects using this approach.

http://msdn.microsoft.com/en-us/magazine/cc163473.aspx

http://www.codeproject.com/KB/database/xp_pcre.aspx

Functions using the PATINDEX operator:

CREATE FUNCTION dbo.IsAlphaNumeric(
    @input VARCHAR(MAX)
)
RETURNS BIT
AS
BEGIN
    -- allows spaces too
    DECLARE @result BIT = 1 -- default result to true
    IF (PATINDEX('%[^a-Z,0-9,'' '']%', @input) = 0)
    BEGIN
        SET @result = 0 -- found a non-alphanumeric character
    END
    RETURN @result -- return result
END

You should look at the SQL Server built in ISNUMERIC function first before building your own:
http://msdn.microsoft.com/en-us/library/ms186272.aspx

CREATE FUNCTION dbo.IsNumeric(
    @input VARCHAR(MAX)
)
RETURNS BIT
AS
BEGIN
    DECLARE @result BIT = 1 -- default result to true
    IF (PATINDEX('%[^0-9]%', @input) = 0)
    BEGIN
        SET @result = 0 -- found a non-alphanumeric character
    END
    RETURN @result -- return result
END
CREATE FUNCTION dbo.IsAlpha(
    @input VARCHAR(MAX)
)
RETURNS BIT
AS
BEGIN
    -- allows spaces too
    DECLARE @result BIT = 1 -- default result to true
    IF (PATINDEX('%[^a-Z,'' '']%', @input) = 0)
    BEGIN
        SET @result = 0 -- found a non-alphanumeric character
    END
    RETURN @result -- return result
END

A function using the LIKE operator:

CREATE FUNCTION dbo.IsAlphaNumeric(
    @input VARCHAR(MAX)
)
RETURNS BIT
AS
BEGIN
    -- allows spaces too
    DECLARE @result BIT = 1 -- default result to true
    IF (@input LIKE '%[^a-Z,0-9,'' '']%')
    BEGIN
        SET @result = 0 -- found a non-alphanumeric character
    END
    RETURN @result -- return result
END


Viewing all articles
Browse latest Browse all 10

Trending Articles