根据分隔符解析列值

发布于 2024-09-28 16:29:07 字数 502 浏览 0 评论 0原文

这是我的数据示例:

ABC*12345ABC
BCD*234()
CDE*3456789(&(&
DEF*4567A*B*C

使用 SQL Server 2008 或 SSIS,我需要解析此数据并返回以下结果:

12345
234
3456789
4567

如您所见,星号 (*) 是我的第一个分隔符。第二个“分隔符”(我宽松地使用这个术语)是当数字序列停止时。

所以,基本上,只需抓住星号后面的数字序列......

我怎样才能做到这一点?

编辑:

我在原来的帖子中犯了一个错误。另一个可能值的示例是:

XWZ*A12345%$%

在这种情况下,我想返回以下内容:

A12345

该值可以以字母字符开始,但始终以数字结束。因此,抓住星号后面的所有内容,但停在序列中的最后一个数字上。

任何对此的帮助将不胜感激!

Here is a sample of my data:

ABC*12345ABC
BCD*234()
CDE*3456789(&(&
DEF*4567A*B*C

Using SQL Server 2008 or SSIS, I need to parse this data and return the following result:

12345
234
3456789
4567

As you can see, the asterisk (*) is my first delimiter. The second "delimiter" (I use this term loosely) is when the sequence of numbers STOP.

So, basically, just grab the sequence of numbers after the asterisk...

How can I accomplish this?

EDIT:

I made a mistake in my original post. An example of another possible value would be:

XWZ*A12345%$%

In this case, I would like to return the following:

A12345

The value can START with an alpha character, but it will always END with a number. So, grab everything after the asterisk, but stop at the last number in the sequence.

Any help with this will be greatly appreciated!

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

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

发布评论

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

评论(2

疑心病 2024-10-05 16:29:07

您可以使用一点 patindexcharindex 技巧来做到这一点,例如:

; with YourTable(col1) as 
        (
        select 'ABC*12345ABC'
        union all select 'BCD*234()'
        union all select 'CDE*3456789(&(&'
        union all select 'DEF*4567A*B*C'
        union all select 'XWZ*A12345%$%'
        )
select  left(AfterStar, len(Leader) + PATINDEX('%[^0-9]%', AfterLeader) - 1)
from    (
        select  RIGHT(AfterStar, len(AfterStar) - PATINDEX('%[0-9]%', AfterStar) + 1) 
                    as AfterLeader
        ,       LEFT(AfterStar, PATINDEX('%[0-9]%', AfterStar) - 1) as Leader
        ,       AfterStar
        from    (
                select  RIGHT(col1, len(col1) - CHARINDEX('*', col1)) as AfterStar
                from    YourTable
                ) as Sub1
        ) as Sub2

这会打印:

12345
234
3456789
4567
A12345

You could do this with a little patindex and charindex trickery, like:

; with YourTable(col1) as 
        (
        select 'ABC*12345ABC'
        union all select 'BCD*234()'
        union all select 'CDE*3456789(&(&'
        union all select 'DEF*4567A*B*C'
        union all select 'XWZ*A12345%$%'
        )
select  left(AfterStar, len(Leader) + PATINDEX('%[^0-9]%', AfterLeader) - 1)
from    (
        select  RIGHT(AfterStar, len(AfterStar) - PATINDEX('%[0-9]%', AfterStar) + 1) 
                    as AfterLeader
        ,       LEFT(AfterStar, PATINDEX('%[0-9]%', AfterStar) - 1) as Leader
        ,       AfterStar
        from    (
                select  RIGHT(col1, len(col1) - CHARINDEX('*', col1)) as AfterStar
                from    YourTable
                ) as Sub1
        ) as Sub2

This prints:

12345
234
3456789
4567
A12345
淡墨 2024-10-05 16:29:07

如果您忽略这是在 SQL 中,那么首先想到的是 Regex:

^.*\*(.*[0-9])[^0-9]*$

那里的捕获组应该得到您想要的内容。我不知道SQL是否有正则表达式功能。

If you ignore that this is in SQL then the first thing that comes to mind is Regex:

^.*\*(.*[0-9])[^0-9]*$

The capture group there should get what you want. I don't know if SQL has a regex function.

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