You can use charindex(<what>, <where>)
function. First, to find index of AQISPremIdProcessType_Hidden=
string. Second, to find index of first *
after it.
Here an example:
declare
@testString nvarchar(max) = N'AQISEntityIds_Hidden=CN0001MB*AQISPremIdProcessType_Hidden=N2725/D*CPQuestionGenDate_Hidden=20/01/2017',
@field nvarchar(max) = N'AQISPremIdProcessType_Hidden=';
;with _rawInfo as(
select @testString as [Str]
)
, _trimmedStart as (
select
[Str] as [Origin],
-- check here the math: 2nd arg = length of the tail, so we take
-- whole length [StrLen] and subtract index of @field and its length
right([Str], [StrLen] - FieldInd - FieldLen + 1) as Tail
from _rawInfo
cross apply (
select
len([Str]) as [StrLen],
charindex(@field, [Str]) as FieldInd,
len(@field) as FieldLen
) x
), _result as (
select
[Origin],
Tail,
-- 2nd arg = length of the left cut, so we just take index of '*'
left(Tail, charindex(N'*', Tail) - 1) as [Value]
from _trimmedStart
)
select
*
from _result
UPD. And query adapted for your data:
;with _trimmedStart as (
select
jd.*,
right(AddInfo, AddInfoLen - FieldInd - FieldLen + 1) as AddInfoTail
from JobDeclaration jd
cross apply (
select
len(AddInfo) as AddInfoLen,
charindex(@field, AddInfo) as FieldInd,
len(@field) as FieldLen
) x
), _result as (
select
t.*,
left(AddInfoTail, charindex(N'*', AddInfoTail) - 1) as [AQISPremIdProcessType_Hidden]
from _trimmedStart t
)
select
*
from _result