【问题标题】:How can I get values between the last two forward slashes (/) in URL column? in SQL如何获取 URL 列中最后两个正斜杠 (/) 之间的值?在 SQL 中
【发布时间】:2017-06-11 20:19:04
【问题描述】:

您好,提前感谢您的帮助!

我有一个名为“URL”的列,需要解析出DocID,在它下面的返回值是“87”。

http://SiteName-Domain/Announcements/Attachments/**87**/Document.DOCX

如何查询最后两个正斜杠(/)之间的值来获取DocId?

我可以得到最后一个斜线之后的值,但不知道如何在最后两个之间:

Select RIGHT(ax.URL, (CHARINDEX('/',REVERSE(ax.URL),0))-1),* from     SPListItemAttachments ax

这是另一个尝试接近但我做错了什么

SELECT SUBSTRING(URL, CHARINDEX('/', URL)
, CHARINDEX('/',URL) - CHARINDEX('/', URL) + Len('/')) from SPListItemAttachments

【问题讨论】:

  • 您是否缺少一些标签?
  • 请标记您的 dbms 并向我们展示您的尝试。
  • 如果我们是新的,在您的脚本语言中执行此操作也可能是微不足道的。
  • 一切就绪。添加了我的尝试。

标签: sql parsing substring reverse


【解决方案1】:

在 SQL Server 中尝试不使用 Len 和 substr 的逻辑

declare @mytext varchar(max) = 'http://SiteName-Domain/Announcements/Attachments/**87**/Document.DOCX'

--解释--

select charindex('/',reverse(@mytext)) -- get 1st position of /
select right(@mytext,charindex('/',reverse(@mytext))) -- get the text from right
select replace(@mytext,right(@mytext,charindex('/',reverse(@mytext))),'') -- replace the first right text to get extract what you are after of/

-- 实际上是你的脚本--

select  right(replace(@mytext,right(@mytext,charindex('/',reverse(@mytext))),''),  charindex('/',reverse(replace(@mytext,right(@mytext,charindex('/',reverse(@mytext))),'')))-1)  -- then extract from the right text after /

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    相关资源
    最近更新 更多