TSQL 电子邮件验证(无正则表达式)

TSQL Email Validation (without regex)(TSQL 电子邮件验证(无正则表达式))
本文介绍了TSQL 电子邮件验证(无正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,有上百万个正则表达式可用于验证电子邮件地址,但如何将一些基本的电子邮件验证集成到 Sql Server 2005 的 TSQL 查询中?

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 2005?

我不想使用 CLR 过程或函数.只是直接的 TSQL.

I don't want to use a CLR procedure or function. Just straight TSQL.

有人已经解决了吗?

推荐答案

非常:

SELECT
  EmailAddress, 
  CASE WHEN EmailAddress LIKE '%_@_%_.__%' 
            AND EmailAddress NOT LIKE '%[any obviously invalid characters]%' 
  THEN 'Could be' 
  ELSE 'Nope' 
  END Validates
FROM 
  Table

这匹配中间有 @ 的所有内容,前面至少有一个字符,后跟至少两个字符,一个点,至少两个用于 TLD.

This matches everything with an @ in the middle, preceded by at least one character, followed by at least two, a dot and at least two for the TLD.

您可以编写更多 LIKE 模式来做更具体的事情,但是您永远无法匹配可能是电子邮件地址的所有内容,同时又不会漏掉不是电子邮件地址的内容.即使使用正则表达式,您也很难做到正确.此外,即使根据 RFC 的字母进行匹配,也会匹配大多数电子邮件系统不会接受/使用的地址结构.

You can write more LIKE patterns that do more specific things, but you will never be able to match everything that could be an e-mail address while not letting slip through things that are not. Even with regular expressions you have a hard time doing it right. Additionally, even matching according to the very letters of the RFC matches address constructs that will not be accepted/used by most emailing systems.

无论如何,在数据库级别执行此操作可能是错误的方法,因此如上所述的基本健全性检查可能是您在性能方面所能获得的最佳结果,而在应用程序中执行此操作将为您提供更大的灵活性.

Doing this on the database level is maybe the wrong approach anyway, so a basic sanity check as indicated above may be the best you can get performance-wise, and doing it in an application will provide you with far greater flexibility.

这篇关于TSQL 电子邮件验证(无正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)