SQL-如何匹配从字符串内的随机索引开始的字符串模式

发布于 2025-01-01 10:52:35 字数 240 浏览 3 评论 0原文

我有以下模式;

AA(AA)–NBXXYYY–CCCCCCCC(CC)–DD(D)

其中括号中的值是可选的,并且可能不存在。

我只想选择字符串的 XX 部分。可以通过巧妙地使用子字符串来完成吗?如果有任何好的意见,我将不胜感激。

编辑: 一个提示可能是我总是可以跳过第一个连字符(-)之前的内容,它总是在那里......但仍然不确定如何实现它。

I have the following pattern;

AA(AA)–NBXXYYY–CCCCCCC(CC)–DD(D)

Where the values in brackets are optional and may not exist.

I want to select only the XX part of the string. Could it be done with clever use of substring? I would appreciate any good input here.

Edit:
A hint might be that I can always skip the stuff before the first hyphen (-), which will always be there... but still not sure how to implement it.

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

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

发布评论

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

评论(2

南冥有猫 2025-01-08 10:52:35
declare @S varchar(100) = 'AA(AA)-NBXXYYY-CCCCCCC(CC)-DD(D)'

-- First two characters after –NB
select substring(@S, charindex('-NB', @S)+3, 2)

-- Or if it is not always NB just find the first –
select substring(@S, charindex('-', @S)+3, 2)

使用您的样本数据:

;with C(col) as
(
  select '10-1R22345-33PY101-N4' union all
  select '100-1R22345-33PY101Z-N4' union all
  select '1000-1R22345-33PY101ZZ-N4'
)

select substring(col, charindex('-', col)+3, 2)
from C
declare @S varchar(100) = 'AA(AA)-NBXXYYY-CCCCCCC(CC)-DD(D)'

-- First two characters after –NB
select substring(@S, charindex('-NB', @S)+3, 2)

-- Or if it is not always NB just find the first –
select substring(@S, charindex('-', @S)+3, 2)

With your sample data:

;with C(col) as
(
  select '10-1R22345-33PY101-N4' union all
  select '100-1R22345-33PY101Z-N4' union all
  select '1000-1R22345-33PY101ZZ-N4'
)

select substring(col, charindex('-', col)+3, 2)
from C
夏末的微笑 2025-01-08 10:52:35

您可以用 C# 编写一个方法来解析文本字段。无论你做什么,无论如何你都在表扫描区域中 - 没有索引将起作用。因此,如果 SQL 限制了您,请继续使用集成的 .NET 运行时。

You can write a method in C# to do the parsing otf the text field. Regardless what you do you are in a table scan area anyway - no index will work. So, if SQL limits you, just move on and use the integrated .NET runtime.

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