删除字符串SQL的最后一部分

时间:2015-07-29 12:05:15

标签: sql string function

我有一个名为Path的字段,它看起来像这样:

val (name, date) =
  for {
    (table1, table2) <- Customer leftJoin Transaction on (_.custID === _.custID)
    if table1.name === "ABCD"          
  } yield (table1.name, table2.date)

我需要一个参数,我可以从中选择文件夹名称。像这样:

/{e8cfdcba-9572-4c64-828f-dea54d8a00b7}/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl

我现在所做的就是删除路径的第一位。这是代码:

/sites/BI_Center/euroreporting/Reports/BITraining/Elena

现在,我的路径如下:SELECT replace(reverse(substring(reverse(Path), 1, ISNULL(NullIF(charindex('}',reverse(Path)),0),len(Path))) ),'}','') AS Path2 from Catalog

如何排除报告的名称? (例如GroupingEx.rdl)。我尝试了MID功能,但它不起作用,因为报告的名称长度是可变的。

提前谢谢。

3 个答案:

答案 0 :(得分:3)

这是方法之一

declare @s varchar(200)
set @s='/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select reverse(replace(reverse(@s),substring(reverse(@s),1,charindex('/',reverse(@s))),''))

编辑:

这更简单

declare @s varchar(200)
set @s='/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select substring(@s,1,len(@s)-charindex('/',reverse(@s)))

答案 1 :(得分:0)

我建议这个功能:

SUBSTRING([path], CHARINDEX('/', [path], 2), 
          LEN([path]) - CHARINDEX('/', [path], 2) - CHARINDEX('/', REVERSE([path]), 1) + 1)

为此:/sites/BI_Center/euroreporting/Reports/BITraining/Elena

答案 2 :(得分:0)

declare @str varchar(250)
set @str = '/{e8cfdcba-9572-4c64-828f-dea54d8a00b7}/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select substring ( 
        @str, --expression
        charindex('}', @str) + 1, --start
        len(@str) - charindex('/', reverse(@str)) - charindex('}', @str) --how many characters of the expression will be returned
    ) 

输出:

/sites/BI_Center/euroreporting/Reports/BITraining/Elena