我被困在这里,非常感谢任何帮助!
R:£30 AT:63 RT:0 D .ADD£400 @ 63 WK
SQL任务:
1 - 检索400(找到符号@并向左移动多个字符,直到达到£符号)
2 - 检索63(找到@符号并获得尽可能多的字符,直到找到“”或“W”
答案 0 :(得分:1)
只需使用charIndex和substring。下面的例子假设在@之后也可以有£。基本上,将字符串拆分为@,对于第二部分,我将从@转到''。第一部分,保留它,找到£,然后反转它。
declare @col varchar(500)
set @col = 'R:£30 AT:63 RT:0 D .ADD £400 @63 WK'
declare @p1 varchar(500),@p2 varchar(500) --split col into 2 at @
set @p1 = (reverse(substring(@col,1,CHARINDEX('@',@col)-1))) -- i will reverse here
set @p2 = (substring(@col,CHARINDEX('@',@col)+1,LEN(@col)))
select @p1 p1, @p2 p2
,ltrim(rtrim(reverse(substring(@p1,1,CHARINDEX('£',@p1)-1)))) p1Final -- do the same thing as we did to p1 and reserse it
--also do a trim left and right to get rid of extra spaces
,ltrim(rtrim(SUBSTRING(@p2,1,charIndex(' ',@p2)))) p2Final --this one should be self explanatory if you get the first one :)
答案 1 :(得分:1)
您还可以使用可以查找模式的patindex函数。
declare @col varchar(50)
set @col='R:£30 AT:63 RT:0 D .ADD £400 @63 WK'
--400
select substring(@col,patindex('% £% @%',@col)+2,charindex('@',@col)-(patindex('% £% @%',@col)+3))
--63
select substring(@col,charindex('@',@col)+1,charindex(' ',reverse(@col))-1)
答案 2 :(得分:0)
不确定效率,但要让你开始
对于1:我颠倒了弦并操纵它然后将结果反转回来
declare @s varchar(500) = 'R:£30 AT:63 RT:0 D .ADD £23 £400 @63 WK'
declare @sRev varchar(500) = REVERSE(@s)
declare @stemp varchar(500)
declare @ampIndRev int, @AfterAmpIndRev int
set @ampIndRev = CHARINDEX( '@',@sRev,1)
set @AfterAmpIndRev = charindex('£',@sRev, @ampIndRev)
set @stemp = SUBSTRING(@sRev, @ampIndRev + 1, @AfterAmpIndRev-@ampIndRev-1)
set @stemp = REVERSE(Ltrim(@stemp))
select @stemp
对于2(我假设你只有在没有空间时才需要寻找W):
declare @s varchar(500) = 'R:£30 AT:63 RT:0 D .ADD £400 @63 WK'
declare @ampInd int, @AfterAmpInd int
set @ampInd = CHARINDEX( '@',@s,1)
set @AfterAmpInd = CHARINDEX(' ',@s,@ampInd)
if @AfterAmpInd = 0
set @AfterAmpInd = CHARINDEX('W',@s,@ampInd)
select SUBSTRING(@s, @ampInd + 1, @AfterAmpInd-@ampInd-1)