我需要一个SQL查询来从一个主字符串中获取两个字符串,返回的值以T#######@@###@@####
开头。主字符串长度发生变化。
示例:
主要字符串
@ code = 025121710TestPASS * 68242850AD * 68242382AF * 1UJ97DX9AF * 68248793AB * 68236772AB * 56054275AG * NoPN * 1UW38DX9AC NoPN T00BE161571394 * T8LQI141529458 * NoPN NoPN NoPN * NoPN
捕获的第一个子字符串
T00BE161571394
捕获第二个子字符串
T8LQI141529458
到目前为止,我已经想出了这个,但无济于事:
捕获的第一个子字符串
SELECT left(RIGHT(code, 51), 15)
捕获第二个子字符串
SELECT left(RIGHT(code, 35), 15)
有人可以帮帮我吗?我不确定如何正确地考虑长度并以正确的顺序分离子串。
答案 0 :(得分:3)
我猜你可以使用[*]分开。反向字符串和拆分
declare @string1 nvarchar(250)
declare @string2 nvarchar(250)
declare @myString nvarchar(500)= '025121710TestPASS*68242850AD*68242382AF*1UJ97DX9AF*68248793AB*68236772AB*56054275AG*NoPN*1UW38DX9ACNoPNT00BE161571394 *T8LQI141529458*NoPNNoPNNoPN*NoPN'
set @myString = REVERSE(@myString)
DECLARE @StartPos int, @Length int
select @StartPos = CHARINDEX('*', @myString)
set @myString = SUBSTRING(@myString,(@StartPos+1),len(@myString)-@StartPos)
select @StartPos = CHARINDEX('*', @myString)
set @myString = SUBSTRING(@myString,(@StartPos+1),len(@myString)-@StartPos)
select @StartPos = CHARINDEX('*', @myString)
select @string1 = SUBSTRING(@myString,0,@StartPos)
set @myString = SUBSTRING(@myString,(@StartPos+1),len(@myString)-@StartPos)
select @StartPos = CHARINDEX('*', @myString)
select @string2 = SUBSTRING(@myString,0,16)
select REVERSE(@string1) ,REVERSE(@string2)
答案 1 :(得分:2)
尝试此操作,这将在主字符串中选择完整的子字符串列表
declare @myString nvarchar(500)= '025121710TestPASS*68242850AD*68242382AF*1UJ97DX9AF*68248793AB*68236772AB*56054275AG*NoPN*1UW38DX9ACNoPNT00BE161571394 *T8LQI141529458*NoPNNoPNNoPN*NoPN'
;with T(ind,pos) as (
select charindex('T', @myString), 1
union all
select charindex('T', substring(@myString,ind+1,len(@myString)))+ind,pos+1
from t
where pos > 0 and ind <> charindex('T', substring(@myString,ind+1,len(@myString)))+ind
)
select substring(@myString,ind,14) as YourString from t where substring(@myString,ind,14) NOT LIKE '%[^a-zA-Z0-9]%'