首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL Server CHARINDEX函数

SQL Server CHARINDEX函数
EN

Stack Overflow用户
提问于 2020-06-25 12:04:50
回答 1查看 96关注 0票数 0

我正在寻找一些函数,用for循环或不同的方式替换列中的所有行。该函数需要通过revers从特定字符(从结束处反斜杠)给出子字符串。

以前:'חוזה - דף ראשון#Y:\Access\Shiduhim\Agreem1\999999.Bmp'

需要在方法之后:'Shiduhim\Agreem1\999999.Bmp'

EN

回答 1

Stack Overflow用户

发布于 2020-06-25 18:14:39

您的问题可以通过递归CTE来解决(需要了解maxrecursion):

代码语言:javascript
复制
declare @t table(
  path nvarchar(260)
);

insert into @t(path) values
  (N'חוזה - דף ראשון#Y:\Access\Shiduhim\Agreem1\999999.Bmp'),
  ('C:\abc\defg\hi.png'),
  ('17042.jpg'),
  ('D:\foo\bar.tiff');

declare @n int = 3;

with
  r as (
    select r, 0 as p, 0 as n, iif(charindex('\', r) > 0, 1, 0) as x
    from @t
    cross apply (select reverse(path)) as a(r)
    union all
    select r, a.p, n + 1, iif(n < @n - 1 and charindex('\', r, a.p + 1) > 0, 1, 0)
    from r
    cross apply (select charindex('\', r, p + 1)) as a(p)
    where x = 1
  )
select
  reverse(iif(n < @n, r, left(r, p - 1))) as path
from r
where x = 0;

输出:

代码语言:javascript
复制
+-----------------------------+
|            path             |
+-----------------------------+
| 17042.jpg                   |
| D:\foo\bar.tiff             |
| abc\defg\hi.png             |
| Shiduhim\Agreem1\999999.Bmp |
+-----------------------------+

演示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62574902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档