如何在SQL Server中的交叉应用联接中指定列名

时间:2019-07-11 04:38:49

标签: sql sql-server cross-apply

我想知道我们如何在“交叉应用联接”中提及列名并指定表名

已经可以使用的查询:

select v.*, left(v.dir, charindex('/', v.dir) - 1) as dirname
from (values ('/Directory_A/Directory_B/Directory_C/Folder1/Folder2') ) t(path) cross apply
     (values (stuff(t.path, 1, len(t.path) - charindex(reverse('Directory_'), reverse(t.path)) + 1, ''))) v(dir)

预期查询:

select v.*, left(v.dir, charindex('/', v.dir) - 1) as dirname
from (values (“ColumnName”) ) t(path) cross apply
     (values (stuff(t.path, 1, len(t.path) - charindex(reverse('Directory_'), reverse(t.path)) + 1, ''))) v(dir) from table name

2 个答案:

答案 0 :(得分:0)

它将为您服务

DECLARE @path VARCHAR(100)='/Directory_A/Directory_B/Directory_C/Folder1/Folder2'

SELECT SUBSTRING( @path,LEN(@path)-LEN(SUBSTRING(@path,PATINDEX('%folder%',@path), 100))-1, 1)

答案 1 :(得分:0)

我认为您只需要表引用:

select v.*, left(v.dir, charindex('/', v.dir) - 1) as dirname
from <tablename> tab cross apply
     (values (tab.ColumnName) ) t(path) cross apply
     (values (stuff(t.path, 1, len(t.path) - charindex(reverse('Directory_'), reverse(t.path)) + 1, ''))) v(dir) from table name