SQL查询中的有效记录

发布于 2024-08-23 08:08:37 字数 208 浏览 4 评论 0原文

我有一个包含几列的表,其中一列是 DockNumber。如果它们确认为特定格式,我必须显示码头号码

前五个字符是数字,后跟 - 并后跟 5 个字符。最后一个字符应该是 alpha。

12345-678V9

如果前 5 个字符是数字并且有一个连字符,接下来的 3 个字符是数字,最后一个是字母,我如何在 SQL 中检查。

I have a table with few columns and one of the column is DockNumber. I have to display the docknumbers if they confirm to a particular format

First five characters are numbers followed by a - and followed by 5 characters. The last but one character should be a alpha.

12345-678V9

How can I check in SQL if the first 5 characters are numbers and there is a hyphen and next 3 are numbers and last but one is an alpha.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无人接听 2024-08-30 08:08:37

基于@gbn的答案,这会检查以确保长度为11(如果@val不是char(11)或varchar(11),并且还检查以确保倒数第二个字符是alpha

DECLARE @val VARCHAR(20)
SET @val = '12345-678V9'
SELECT  CASE WHEN LEN(@val) = 11 AND @val LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z0-9][0-9]' 
        THEN 'isMatch'
        ELSE 'isNotMatch'
    END AS [Valid]

Building on @gbn's answer, this checks to make sure the length is 11 (in case the @val is not a char(11) or varchar(11) and also checks to make sure the second to last char is alpha

DECLARE @val VARCHAR(20)
SET @val = '12345-678V9'
SELECT  CASE WHEN LEN(@val) = 11 AND @val LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z0-9][0-9]' 
        THEN 'isMatch'
        ELSE 'isNotMatch'
    END AS [Valid]
甚是思念 2024-08-30 08:08:37

你可以使用这个,你必须弄清楚如何使用它......

SELECT Case when 
Cast(ISNUMERIC(LEFT(@Str,5)) as int) + case when substring(@str,6,1)= '-' then 1 else 0 end +case when substring(@str,10,1) like '[a-z]' then 1 else 0 end =3
THEN 'Matched'
Else 'NotMatched'
End

you can use this, you will have to figure it out on how to use this...

SELECT Case when 
Cast(ISNUMERIC(LEFT(@Str,5)) as int) + case when substring(@str,6,1)= '-' then 1 else 0 end +case when substring(@str,10,1) like '[a-z]' then 1 else 0 end =3
THEN 'Matched'
Else 'NotMatched'
End
一张白纸 2024-08-30 08:08:37

正则表达式可以成为您的朋友。

Regular Expressions can be your friend.

不喜欢何必死缠烂打 2024-08-30 08:08:37
LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z][0-9]'

现在,这也允许小写 az。如果你只想要大写,你需要强制排序

Value COLLATE Latin_General_BIN
       LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z][0-9]' COLLATE Latin_General_BIN
LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z][0-9]'

Now, this allows lower case a-z too. You'd need to coerce collation if you wanted upper case only

Value COLLATE Latin_General_BIN
       LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z][0-9]' COLLATE Latin_General_BIN
冷月断魂刀 2024-08-30 08:08:37

PATINDEX 可能是理想的解决方案。

Select ...
From Table
Where PatIndex('[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z][0-9]', DockNumber) > 0

PATINDEX is probably the ideal solution.

Select ...
From Table
Where PatIndex('[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][A-Z][0-9]', DockNumber) > 0
心头的小情儿 2024-08-30 08:08:37
Where  DockNumber Like '[0-9][0-9][0-9][0-9][0-9][-][0-9][0-9][0-9][a-z][0-9]

应该可以,但我建议在代码中使用正则表达式。如果可能的话会容易得多。

Where  DockNumber Like '[0-9][0-9][0-9][0-9][0-9][-][0-9][0-9][0-9][a-z][0-9]

should work, but i would suggest using Regular expression in code. Much easier if it is possible.

离去的眼神 2024-08-30 08:08:37

正则表达式应该是 '^\d{5}-\d{3}[AZ]\d$',因为没有 ^$ 它将找到包含该序列的较长字符串 (122 12345-678V9 34)。

The regex should be '^\d{5}-\d{3}[A-Z]\d$', because without ^ and $ it would find longer strings that contain that sequence (122 12345-678V9 34).

蓝天 2024-08-30 08:08:37

使用规则

CREATE RULE pattern_rule 
AS
@value LIKE '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][A-Z][0-9]'

然后将规则绑定到列

Use rule

CREATE RULE pattern_rule 
AS
@value LIKE '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][A-Z][0-9]'

Then bind rule to column

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文