如何使用 SQL 模式匹配检查特定字符串的结尾?
我正在尝试使用 sql 模式匹配来检查字符串值的格式是否正确。
字符串代码应具有正确的格式:
alphanumericvalue.alphanumericvalue
因此,以下是有效代码:
D0030.2190
C0052.1925
A0025.2013
以下是无效代码:
D0030
.2190
C0052.
A0025.2013.
A0025.2013.2013
到目前为止,我有以下 SQL IF 子句来检查字符串是否正确:
IF @vchAccountNumber LIKE '_%._%[^.]'
我相信“ _%”部分检查 1 个或多个字符。因此,该语句检查一个或多个字符,后跟“.”。字符,后跟一个或多个字符,并检查最后一个字符不是“.”。
似乎这适用于所有组合,除了 IF 子句允许作为有效代码的以下格式之外:
A0025.2013.2013
我无法更正此 IF 子句以允许它将此格式视为不正确。有人可以帮我纠正这个问题吗?
谢谢。
I am trying to use sql pattern matching to check if a string value is in the correct format.
The string code should have the correct format of:
alphanumericvalue.alphanumericvalue
Therefore, the following are valid codes:
D0030.2190
C0052.1925
A0025.2013
And the following are invalid codes:
D0030
.2190
C0052.
A0025.2013.
A0025.2013.2013
So far I have the following SQL IF clause to check that the string is correct:
IF @vchAccountNumber LIKE '_%._%[^.]'
I believe that the "_%" part checks for 1 or more characters. Therefore, this statement checks for one or more characters, followed by a "." character, followed by one or more characters and checking that the final character is not a ".".
It seems that this would work for all combinations except for the following format which the IF clause allows as a valid code:
A0025.2013.2013
I'm having trouble correcting this IF clause to allow it to treat this format as incorrect. Can anybody help me to correct this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个stackoverflow问题提到使用单词边界:
[[ :<:]]
和[[:>:]]
用于整个单词匹配。您也许可以使用它,因为您的代码中没有空格。This stackoverflow question mentions using word-boundaries:
[[:<:]]
and[[:>:]]
for whole word matches. You might be able to use this since you don't have spaces in your code.这是 ANSI SQL 解决方案。
此 LIKE 表达式将找到不
alphanumeric.alphanumeric
的任何模式。所以不喜欢只找到与您希望匹配的这个:但是,根据您的示例,您可以使用这个...
...或者这样的一个,如果您
5个字母,点,4个字母
对于固定长度值,第二个稍微明显一些。第一个稍微不太直观,但适用于点两侧的可变长度代码。
其他SO问题在SQL Server中创建一个函数,以电话号码作为参数并返回随机数和SQL Server 中 IsInteger 的最佳等效项
This is ANSI SQL solution
This LIKE expression will find any pattern not
alphanumeric.alphanumeric
. So NOT LIKE find only this that match as you wish:However, based on your examples, you can use this...
...or one like this if you
5 alphas, dot, 4 alphas
The 2nd one is slightly more obvious for fixed length values. The 1st one is slighty less intuitive but works with variable length code either side of the dot.
Other SO questions Creating a Function in SQL Server with a Phone Number as a parameter and returns a Random Number and Best equivalent for IsInteger in SQL Server