代码之家  ›  专栏  ›  技术社区  ›  Liam neesan

在SQL Server中如何用正斜杠拆分字符串[[副本]

  •  -1
  • Liam neesan  · 技术社区  · 6 年前

    SQL Server 2012中没有我正在使用的默认拆分函数。

    我想拆分字符串(例如: /Folder1/Folder2/ )由 /

    • 如果字符串是 /Folder1/ Folder1 ,
    • 如果字符串是 那么输出应该是 Folder2
    • 如果字符串是 /Folder1/Folder2/Folder3/ Folder3 .
    1 回复  |  直到 6 年前
        1
  •  1
  •   Michał Turczyn    6 年前

    试试这个:

    declare @tbl table (path varchar(100));
    insert into @tbl values
    ('/Folder1/'),
    ('/Folder1/Folder2/'),
    ('/Folder1/Folder2/Folder3/');
    
    select *, 
           replace(substring(path, len(path) - charindex('/', reverse(path), 2) + 1, 1000), '/', '')
    from @tbl