如何在SQL Server中通过正斜杠拆分字符串?

时间:2018-09-30 09:21:54

标签: sql sql-server split sql-server-2012 substring

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

我想用/Folder1/Folder2/分割字符串(例如:/)。

  • 如果字符串为/Folder1/,则输出应为Folder1
  • 如果字符串为/Folder1/Folder2/,则输出应为Folder2
  • 如果字符串为/Folder1/Folder2/Folder3/,则输出应为Folder3

1 个答案:

答案 0 :(得分:1)

尝试一下:

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