有人可以建议我从SQL Server表中的0459
字符串中提取0460
和(&0459&/&0460&)*100
的方法吗?
我应该能够拉出夹在两对&符号之间的两根琴弦。
提前致谢。
答案 0 :(得分:1)
如果您确定总有两对&符号,您可以像这样提取两个字符串
declare @s varchar(max) = '(&0459&/&0460&)*100'
declare @first int = charindex('&', @s)
declare @second int = charindex('&', @s, @first+1)
declare @third int = charindex('&', @s, @second+1)
declare @fourth int = charindex('&', @s, @third+1)
select substring(@s, @first+1, @second-@first-1)
select substring(@s, @third+1, @fourth-@third-1)
答案 1 :(得分:1)
此解决方案将在'&'
之间绘制第1和第3个值;WITH CTE AS
(
SELECT t.c.value('.', 'VARCHAR(2000)') v,
rn = row_number() over (order by (select 1))-1
FROM (
SELECT x = CAST('<t>' +
REPLACE('(&0459&/&0460&)*100', '&', '</t><t>') + '</t>' AS XML)
) a
CROSS APPLY x.nodes('/t') t(c)
)
SELECT v FROM CTE
WHERE rn in (1,3)
结果:
0459
0460