sql - SQL Server CHARINDEX 函数
问题描述
我正在寻找一些用 for 循环或其他方式替换列中所有行的函数。该函数需要通过反向给出特定字符的子字符串(从末尾开始的第三个反斜杠)
前 :'חוזה - דף ראשון#Y:\Access\Shiduhim\Agreem1\999999.Bmp'
需要在方法之后:'Shiduhim\Agreem1\999999.Bmp'
解决方案
您的问题可以通过递归 CTE解决(需要了解maxrecursion
):
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;
输出:
+-----------------------------+
| path |
+-----------------------------+
| 17042.jpg |
| D:\foo\bar.tiff |
| abc\defg\hi.png |
| Shiduhim\Agreem1\999999.Bmp |
+-----------------------------+
演示。
推荐阅读
- redis - AWS RedisConnectionException:没有可用于服务此操作的连接。套接字关闭异常
- php - 一个视图中多个@foreach的Laravel分页
- python - 我正在尝试通过 Python 中的输入语句通过行索引删除 SQLite3 表中的一行
- firebase - get 方法擦除所有节点?
- javascript - 如何使:在搜索栏中单击/聚焦后保持悬停外观
- google-apps-script - 如何阻止来自 apps-scripts-notifications@google.com、garbageCollectorViewer 的电子邮件?
- optimization - 多于 1 个变量的优化
- python - python matplotlib 如何绘制直方图
- xml - 在 XDocument 中检索带有标签的节点
- cmake - CMake / GCC:CMAKE_C_FLAGS 似乎没有效果