【问题标题】:Removing a part of URL from coulmn in SQL从 SQL 中的列中删除部分 URL
【发布时间】:2022-01-22 16:31:27
【问题描述】:

我在表格中有一个 URL 列,下面是 URL。我想删除 Location 之后的字符串。

https://xyz.sharepoint.com/sites/tender/lp/46/Lists/PlaceDetails/Location3
https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location2/4_.000
https://xyz.sharepoint.com/sites/tender/lp/52/Lists/PlaceDetails/Location5
https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location6/8_.000

预期输出

https://xyz.sharepoint.com/sites/tender/lp/46/Lists/PlaceDetails/Location3
https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location2
https://xyz.sharepoint.com/sites/tender/lp/52/Lists/PlaceDetails/Location5
https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location6

尝试使用charindex,但未能成功。任何建议将不胜感激。

【问题讨论】:

  • 根据您的样本数据,Left(url, 74) 就足够了。但是我怀疑它比这更微妙。你能调整你的样本数据来说明吗?
  • 根据问题指南,请展示您的尝试并告诉我们您发现了什么(在本网站或其他地方)以及为什么它不能满足您的需求。

标签: sql sql-server tsql


【解决方案1】:

你在Location之后检测到“/”的索引,像这样

DECLARE @DATA NVARCHAR(200) = 'https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location2/4_.000'

SELECT CASE 
    WHEN charindex('/', @data, charindex('Location', @data)) = 0
        THEN @data
    ELSE LEFT(@data, charindex('/', @data, charindex('Location', @data)) - 1)
    END

【讨论】:

【解决方案2】:

请尝试以下解决方案。

它使用以下算法:

  1. 将 URL 标记为 XML。
  2. 获取包含“位置”的 XML 元素的位置。
  3. 检索 XML 元素直到上述步骤中的位置,然后重新组合 URL。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, [URL] VARCHAR(MAX));
INSERT INTO @tbl ([URL]) VALUES
('https://xyz.sharepoint.com/sites/tender/lp/46/Lists/PlaceDetails/Location3'),
('https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location2/4_.000'),
('https://xyz.sharepoint.com/sites/tender/lp/52/Lists/PlaceDetails/Location5'),
('https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location6/8_.000');
-- DDL and sample data population, end

DECLARE @separator CHAR(1) = '/'

SELECT t.* 
    , REPLACE(c.query('data(/root/r[position() le sql:column("t2.pos")]/text())')
        .value('.', 'VARCHAR(MAX)')
        ,SPACE(1),@separator) AS Result
FROM @tbl AS t
CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' + 
        REPLACE([URL], @separator, ']]></r><r><![CDATA[') + 
        ']]></r></root>' AS XML)) AS t1(c)
CROSS APPLY (SELECT c.query('for $i in /root/r[contains(.,"Location")]
      let $pos := count(root/*[. << $i]) + 1
      return $pos').value('.','INT')) AS t2(pos)        ;

输出

+----+----------------------------------------------------------------------------+
| ID |                                   Result                                   |
+----+----------------------------------------------------------------------------+
|  1 | https://xyz.sharepoint.com/sites/tender/lp/46/Lists/PlaceDetails/Location3 |
|  2 | https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location2 |
|  3 | https://xyz.sharepoint.com/sites/tender/lp/52/Lists/PlaceDetails/Location5 |
|  4 | https://xyz.sharepoint.com/sites/tender/lp/50/Lists/PlaceDetails/Location6 |
+----+----------------------------------------------------------------------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-13
    • 2015-07-15
    • 2019-11-16
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多