TSQL Email Validation (without regex)

后端 未结 9 638
-上瘾入骨i
-上瘾入骨i 2020-11-27 17:00

Ok, there are a million regexes out there for validating an email address, but how about some basic email validation that can be integrated into a TSQL query for Sql Server

相关标签:
9条回答
  • 2020-11-27 17:38

    This is the easiest way to select them.

    Use this query

    SELECT * FROM <TableName> WHERE [EMail] NOT LIKE '%_@__%.__%'
    
    0 讨论(0)
  • 2020-11-27 17:40
    Create Function [dbo].[fnAppStripNonEmail](@Temp VarChar(1000))
    Returns VarChar(1000)
    AS
    Begin
    
        Declare @KeepValues as varchar(50)
        Set @KeepValues = '%[^a-z,0-9,@,.,-]%'
        While PatIndex(@KeepValues, @Temp) > 0
            Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
    
        Return @Temp
    End
    
    0 讨论(0)
  • 2020-11-27 17:44

    Great answers! Based these recommendations I came up with a simplified function that combines the best 2 answers.

    CREATE FUNCTION [dbo].[fnIsValidEmail]
    (
        @email varchar(255)
    )   
    --Returns true if the string is a valid email address.  
    RETURNS bit  
    As  
    BEGIN
        RETURN CASE WHEN ISNULL(@email, '') <> '' AND @email LIKE '%_@%_.__%' THEN 1 ELSE 0 END
    END
    
    0 讨论(0)
提交回复
热议问题