如何使用SQL子字符串从该文件名中提取字符?
我正在尝试使用子弦函数从文件名中提取名称。
示例文件名是:“ 73186_rhimagnesita_phi_phi_stoploss_truncsssn_nonredact_inc_to_to_apr2022_paid_paid_to_apr2022_edit” 我正在尝试从该文件名提取“ Rhimagnesita”。
我使用的子字符串是:
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - 1)
它给出的结果是:“ rhimagnesita_phi_s”
如何仅使用基因函数提取“ rhimagnesita”?
I'm attempting to use the SUBSTRING function to extract a name out of a filename.
An example filename would be: "73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT"
I'm attempting to extract the "RHIMagnesita" from this filename.
The substring I used was:
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - 1)
The results it gave were: "RHIMagnesita_PHI_S"
How do I extract only "RHIMagnesita" using the Substring function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
子字符串中的第三个参数是长度不是位置,因此您需要提取开始字符串的长度。
您可能需要添加或提取1,但这就是想法。
The third parameter in SUBSTRING is length not position, so you would need to substract the length of the beginning string.
You might need to add or substract 1, but that's the idea.
你很近。您需要使用
charindex
也可以找到第二个下划线的位置。You were close. You need to use
CHARINDEX
to also find the position of the second underscore.这是使用String_split和获取的一种方法,而不是子字符串,我们将字符串分开,仅返回第二行
注:在Azure SQL Server string_split上有一个序数参数,因此您可以编写此信息
Here's a way using STRING_SPLIT and FETCH, rather than SUBSTRING We split the string and only return the second row
Note: On Azure Sql Server STRING_SPLIT has an ordinal parameter, so you could write this