SQL select 已知相同字符之间的字符串
SQL select string between known identical characters
我有代码 select 是典型字符串中第一个和第二个“/”之间的字符串,可能如下所示:
2014-Ceilings/Ceilings/Repair/Asbestos/Supalux
下面的代码正确地 returns 上面例子中的值 'Ceilings'。
SELECT
REPLACE (LEFT(SUBSTRING(ElementPath,CHARINDEX
('/',ElementPath)+1,LEN(ElementPath)),CHARINDEX
('/',SUBSTRING(ElementPath, CHARINDEX
('/',ElementPath)+1,LEN(ElementPath)))),'/','')
FROM K2_Master.dbo.tbElement
如何将它修改为 select 第二个和第三个 '/' 之间的字符串 return 值 'Repair'?
检查这个小技巧是否可行:
DECLARE @String VARCHAR(100)= '2014-Ceilings/Ceilings/Repair/Asbestos/Supalux'
SET @String = '<N>' + Replace(@String, '/', '</N><N>')
+ '</N>'
SELECT c.value('/N[1]', 'varchar(30)'),
c.value('/N[2]', 'varchar(30)'),
c.value('/N[3]', 'varchar(30)')
FROM (SELECT Cast(@String AS XML)) t(c)
SELECT c1.value('.', 'varchar(30)')
FROM (SELECT CAST(@String AS XML)) t(c)
CROSS APPLY c.nodes('/N') AS t1(c1)
SELECT value
FROM (SELECT c1.value('.', 'varchar(30)') AS value,
ROW_NUMBER()
OVER(
ORDER BY (SELECT 1)) rn
FROM (SELECT Cast(@String AS XML)) t(c)
CROSS APPLY c.nodes('/N') AS t1(c1)) temp
WHERE rn = 3
你的情况
SELECT c.value('/N[1]', 'varchar(30)'),
c.value('/N[2]', 'varchar(30)'),
c.value('/N[3]', 'varchar(30)')
FROM (SELECT Cast ('<N>' + Replace( Replace (ElementPath, '/', '</N><N>'),'&','&')
+ '</N>' AS XML)
FROM K2_Master.dbo.tbElement) t(c)
我追问了你的问题,得出了一个冗长的结果..
DECLARE @a VARCHAR(40) = '2014-Ceilings/Ceilings/Repair/Asbestos/Supalux'
SELECT @a
,LEFT(
SUBSTRING(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),CHARINDEX('/',SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),1)+1,LEN(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)))),
CHARINDEX('/',SUBSTRING(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),CHARINDEX('/',SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),1)+1,LEN(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)))),1) - 1
)
结果:
回复你的评论。
DECLARE @String VARCHAR(100)= '2014-Ceilings/Ceilings/Repair/Asbestos/Supalux',
@value INT = 2
DECLARE @itra INT = 1
WHILE @itra <= @value
BEGIN
SET @String = (SELECT SUBSTRING(@String,CHARINDEX('/',@String,1)+1,LEN(@String)))
--SELECT @String
SET @itra = @itra + 1
END
SELECT LEFT(@String, CHARINDEX('/',@String,1) - 1)
把'/'的位置写成@value
然后看结果。对于上面的sql,结果是Repair
。如果你给出 @value = 3,
结果是 Asbestos
我有代码 select 是典型字符串中第一个和第二个“/”之间的字符串,可能如下所示:
2014-Ceilings/Ceilings/Repair/Asbestos/Supalux
下面的代码正确地 returns 上面例子中的值 'Ceilings'。
SELECT
REPLACE (LEFT(SUBSTRING(ElementPath,CHARINDEX
('/',ElementPath)+1,LEN(ElementPath)),CHARINDEX
('/',SUBSTRING(ElementPath, CHARINDEX
('/',ElementPath)+1,LEN(ElementPath)))),'/','')
FROM K2_Master.dbo.tbElement
如何将它修改为 select 第二个和第三个 '/' 之间的字符串 return 值 'Repair'?
检查这个小技巧是否可行:
DECLARE @String VARCHAR(100)= '2014-Ceilings/Ceilings/Repair/Asbestos/Supalux'
SET @String = '<N>' + Replace(@String, '/', '</N><N>')
+ '</N>'
SELECT c.value('/N[1]', 'varchar(30)'),
c.value('/N[2]', 'varchar(30)'),
c.value('/N[3]', 'varchar(30)')
FROM (SELECT Cast(@String AS XML)) t(c)
SELECT c1.value('.', 'varchar(30)')
FROM (SELECT CAST(@String AS XML)) t(c)
CROSS APPLY c.nodes('/N') AS t1(c1)
SELECT value
FROM (SELECT c1.value('.', 'varchar(30)') AS value,
ROW_NUMBER()
OVER(
ORDER BY (SELECT 1)) rn
FROM (SELECT Cast(@String AS XML)) t(c)
CROSS APPLY c.nodes('/N') AS t1(c1)) temp
WHERE rn = 3
你的情况
SELECT c.value('/N[1]', 'varchar(30)'),
c.value('/N[2]', 'varchar(30)'),
c.value('/N[3]', 'varchar(30)')
FROM (SELECT Cast ('<N>' + Replace( Replace (ElementPath, '/', '</N><N>'),'&','&')
+ '</N>' AS XML)
FROM K2_Master.dbo.tbElement) t(c)
我追问了你的问题,得出了一个冗长的结果..
DECLARE @a VARCHAR(40) = '2014-Ceilings/Ceilings/Repair/Asbestos/Supalux'
SELECT @a
,LEFT(
SUBSTRING(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),CHARINDEX('/',SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),1)+1,LEN(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)))),
CHARINDEX('/',SUBSTRING(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),CHARINDEX('/',SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)),1)+1,LEN(SUBSTRING(@a,CHARINDEX('/',@a,1)+1,LEN(@a)))),1) - 1
)
结果:
回复你的评论。
DECLARE @String VARCHAR(100)= '2014-Ceilings/Ceilings/Repair/Asbestos/Supalux',
@value INT = 2
DECLARE @itra INT = 1
WHILE @itra <= @value
BEGIN
SET @String = (SELECT SUBSTRING(@String,CHARINDEX('/',@String,1)+1,LEN(@String)))
--SELECT @String
SET @itra = @itra + 1
END
SELECT LEFT(@String, CHARINDEX('/',@String,1) - 1)
把'/'的位置写成@value
然后看结果。对于上面的sql,结果是Repair
。如果你给出 @value = 3,
结果是 Asbestos