反向子字符串

发布于 2025-01-29 12:26:01 字数 408 浏览 1 评论 0原文

我正在尝试从字符串中提取一些字符。 例如,在(316409953_ND_0214202_000001.pdf)中,我需要在最后一个下划线_和“”之前的字符。 答案:000001 @test = 316409953_ND_0214202_000001.pdf

我尝试过:

REVERSE(SUBSTRING(REVERSE(test),CHARINDEX('.',REVERSE(test))+1,CHARINDEX('_',REVERSE(test)+1)))

我需要在子字符串的最后一部分时提供帮助。我遇到的第一个错误是“转换varchar值'fdp.100000_22024120_dn_359904613'转换时转换失败。” 其次,我只需要在最后一个“ _”之后选择它 请指导或让我知道是否有另一种方法可以做到这一点。

I am trying to extract some characters from a string.
For Example in (316409953_ND_02142022_000001.pdf) I need the characters after the last underscore _ and before the "."
Answer: 000001
@test = 316409953_ND_02142022_000001.pdf

I have tried:

REVERSE(SUBSTRING(REVERSE(test),CHARINDEX('.',REVERSE(test))+1,CHARINDEX('_',REVERSE(test)+1)))

I need help with the last part of substring. First error I am getting is "Conversion failed when converting the varchar value 'fdp.100000_22024120_DN_359904613' to data type int."
Secondly I need it to pick only after the last "_"
Please guide or let me know if there's another way to do this.

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

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

发布评论

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

评论(2

甜中书 2025-02-05 12:26:01

看起来喜欢SQL Server。

在这个假设和使用单个示例值的假设上,您可以尝试follolwing:

declare @test varchar(50) = '316409953_ND_02142022_000001.pdf'

select @test OriginalValue,
   ParseName(Right(@test, CharIndex('_',Reverse(@test))-1),2) ExtractedValue;

This looks like SQL Server.

On that assumption, and using the single example value, you can try the follolwing:

declare @test varchar(50) = '316409953_ND_02142022_000001.pdf'

select @test OriginalValue,
   ParseName(Right(@test, CharIndex('_',Reverse(@test))-1),2) ExtractedValue;
淡写薰衣草的香 2025-02-05 12:26:01

它不是很漂亮,但它可以完成工作; P

declare @str varchar(200) = '316409953_ND_02142022_000001.pdf'
declare @tempStr varchar(200)= (select SUBSTRING(@str,LEN(@str) + 2 - CHARINDEX('_',REVERSE(@str)),LEN(@str)))
set @tempStr = SUBSTRING(@tempStr,1,CHARINDEX('.',@tempStr) - 1)
select @tempStr

it isn't pretty but it does the job ;P

declare @str varchar(200) = '316409953_ND_02142022_000001.pdf'
declare @tempStr varchar(200)= (select SUBSTRING(@str,LEN(@str) + 2 - CHARINDEX('_',REVERSE(@str)),LEN(@str)))
set @tempStr = SUBSTRING(@tempStr,1,CHARINDEX('.',@tempStr) - 1)
select @tempStr
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文