T SQL如何通过删除最后一个分隔符之后的所有部分来修改字符串

发布于 2024-11-16 21:50:08 字数 303 浏览 2 评论 0原文

我正在努力寻找 SQL Server 2008 的正确函数来纠正任何类似以下的字符串: \\myserver\mydir1\dir2\test.txt\\myserver2\dir1\dir2\ dir3\test.txt

最后,结果应如下所示: \\myserver\mydir1\dir2\\myserver2\dir1\dir2\dir3 ,因此在最后一个 " 之后\" ,应该删除子字符串。

知道该怎么做吗?

I'm struggling to find the right functions with SQL Server 2008 to rectify any strings coming like: \\myserver\mydir1\dir2\test.txt or \\myserver2\dir1\dir2\dir3\test.txt.

At the end, the result should appear like: \\myserver\mydir1\dir2 or \\myserver2\dir1\dir2\dir3 , So after the last "\" , the substring should be removed.

Any idea how to do that?

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

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

发布评论

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

评论(4

橙味迷妹 2024-11-23 21:50:08
DECLARE @urls TABLE (URL varchar(2000))

INSERT @urls VALUES ('\\myserver\mydir1\dir2\test.txt')
INSERT @urls VALUES ('\\myserver2\dir1\dir2\dir3\test.txt')

SELECT
    REVERSE(SUBSTRING(REVERSE(URL), CHARINDEX('\', REVERSE(URL))+1, 8000))
FROM
    @urls

逻辑:

  • \\myserver\mydir1\dir2\test.txt
  • REVERSE = txt.tset\2rid\1ridym\revresym\
  • 查找第一个\
  • SUBSTRING = 2rid\1ridym\revresym\
  • REVERSE = \\myserver\mydir1\dir2

您不需要知道SUBSTRING 的字符串的 LEN 所以只需使用 8000

编辑,在关于使用 8000 的评论之后

  1. 您可以在 SUBSTRING 中使用 2147483647,因为它支持最大类型
  2. 只需等于或大于 varchar 变量或列长度
  3. 计算的开销怎么样莱恩?
  4. 实际上,所有 URL 都必须适合 2083 字节
  5. 这有关系吗? “过早优化”等
DECLARE @urls TABLE (URL varchar(2000))

INSERT @urls VALUES ('\\myserver\mydir1\dir2\test.txt')
INSERT @urls VALUES ('\\myserver2\dir1\dir2\dir3\test.txt')

SELECT
    REVERSE(SUBSTRING(REVERSE(URL), CHARINDEX('\', REVERSE(URL))+1, 8000))
FROM
    @urls

Logic:

  • \\myserver\mydir1\dir2\test.txt
  • REVERSE = txt.tset\2rid\1ridym\revresym\
  • look for first \
  • take everything after that SUBSTRING = 2rid\1ridym\revresym\
  • REVERSE = \\myserver\mydir1\dir2

You don't need to know the LEN of the string for the SUBSTRING so just use 8000

Edit, after comment about using 8000

  1. You can use 2147483647 in SUBSTRING because it supports max types
  2. Only has to be equal to or longer that the varchar variable or column length
  3. What about the overhead of calculating LEN?
  4. Practically all URLs have to fit into 2083 bytes
  5. Does it matter? "Premature optimisation" etc
人间☆小暴躁 2024-11-23 21:50:08
DECLARE @input VARCHAR(50)
SET @input = '\\myserver\mydir1\dir2\test.txt'

SELECT SUBSTRING(@input, 1, LEN(@input) - CHARINDEX('\', REVERSE(@input)));
DECLARE @input VARCHAR(50)
SET @input = '\\myserver\mydir1\dir2\test.txt'

SELECT SUBSTRING(@input, 1, LEN(@input) - CHARINDEX('\', REVERSE(@input)));
拥抱影子 2024-11-23 21:50:08
DECLARE @s VarChar(100) = '\myserver\mydir1\dir2\test.txt';

DECLARE @length Int = LEN(@s),
        @position Int = CHARINDEX('\', REVERSE(@s));

SELECT SUBSTRING(@s, 1, @length - @position);
DECLARE @s VarChar(100) = '\myserver\mydir1\dir2\test.txt';

DECLARE @length Int = LEN(@s),
        @position Int = CHARINDEX('\', REVERSE(@s));

SELECT SUBSTRING(@s, 1, @length - @position);
青朷 2024-11-23 21:50:08

尝试这样的事情......

SELECT CASE SUBSTRING ( [yourField], LEN([yourField])-2, LEN([yourField])-1)
WHEN '\' THEN (SUBSTRING ( [yourField], 0, LEN([yourField])-1))
ELSE [yourField]

End
  FROM [yourTable]
GO

希望有帮助,

杰弗里·凯文·普里

Try something like this...

SELECT CASE SUBSTRING ( [yourField], LEN([yourField])-2, LEN([yourField])-1)
WHEN '\' THEN (SUBSTRING ( [yourField], 0, LEN([yourField])-1))
ELSE [yourField]

End
  FROM [yourTable]
GO

Hope that helps,

Jeffrey Kevin Pry

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文