如何使用patindex和substring解析特定值的字符串?
我有以下示例:
declare @test1 varchar(max) = 'Month 05/2022, Ord195506 Cst373175'
declare @test2 varchar(max) = 'Month 05/2022, Ord195506 Cst373175, something...'
select SUBSTRING(@test, PATINDEX('%Ord[0-9][0-9][0-9][0-9][0-9][0-9]%', @test1) + 3, 6)
我需要获取以下值:
- 05/2022
- 195506
- 373175
必须注意,ord> ord> ord
和abo
的数字未固定为长度6。它们可以从1到任何数字。
是否可以将字符串分为必要的值,而无需大量patindex
,substring
和右
?
I have the following example:
declare @test1 varchar(max) = 'Month 05/2022, Ord195506 Cst373175'
declare @test2 varchar(max) = 'Month 05/2022, Ord195506 Cst373175, something...'
select SUBSTRING(@test, PATINDEX('%Ord[0-9][0-9][0-9][0-9][0-9][0-9]%', @test1) + 3, 6)
I need to get the following values:
- 05/2022
- 195506
- 373175
It must be noted that the numbers behind Ord
and Abo
are not fixed to a length of 6. They can be from 1 to any number.
Is it even possible to split the string into required values without a multitude of PATINDEX
, SUBSTRING
and RIGHT
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的问题中的示例,您可以 string_split 在空间上的字符串中的元素,然后使用 translate 删除非数字元素,例如:
For the example in your question you could string_split the elements in the string on the space and then remove the non-numeric elements using translate, such as:
Example DB<>Fiddle