我有一个字符串'"&" abc1"#" 1"&"" abc2"#& #34; 2"&安培;"" ABC3"#" 3"&安培;"" ABC1"#" 4
我只想将每个int提取到单独的变量中。目前我有 注意endtext ="#" 和endnum ="&"
WHILE @N < LEN(@TextField)
BEGIN
SET @read1 = (
SELECT SUBSTRING(@TextFieled,
CHARINDEX(@endtext, @TextFieled) + @N --Increment 2 so skips to next "
, CHARINDEX(@endnum,@TextFieled) - CHARINDEX(@endtext, @TextFieled)) --MANY CHARS TO GO FOR
)
SELECT @read1
SET @N = @N + CHARINDEX(@read1, @TextFieled) --next strating point
END
但它不仅仅返回数字。我认为问题在于我想要选择的nums位于重复的字符串中,因此我不确定如何改变我的循环以跳过。
答案 0 :(得分:1)
为了便于阅读和调试,请使用变量来存储CHARINDEX
DECLARE
@N int = 2,
@TextField varchar(100) = '"&"abc1"#"1"&""abc2"#"2"&""abc3"#"3"&""abc1"#"4',
@read1 varchar(10),
@endtext varchar(3) = '"#"',
@endnum varchar(3) = '"&"',
@Start int,
@Finish int
WHILE @N < LEN(@TextField)
BEGIN
SET @Start = CHARINDEX(@endtext, @TextField, @N)
SET @Finish =CHARINDEX(@endnum, @TextField, @N)
IF @Finish = 0 SET @Finish = LEN(@TextField) + 1
SET @read1 = (SELECT SUBSTRING(@TextField, @Start + 3, @Finish - @Start - 3))
SELECT @read1 xRead
SET @N = @Finish + 1 --next strating point
END