I am using ssms 2016. I have strings with following pattern 14Jan22_B_15May23_15_N_323_412_102
I need to extract from it two things.
- First number between two
_
. In this case it is 15, but it can be any number. I tried below method but it didn't work. it returns 0
DECLARE @InputString VARCHAR(MAX) = '14Jan22_B_15May23_15_N_323_412_102';
DECLARE @MatchPos INT = PATINDEX('%_[0-9]+_%', @InputString);
select @MatchPos
- All numbers after
_N_
or_R_
. Also this_N_
or_R_
needs to be after number from point 1. Because letter B can also be letter N.
I didn't start working on point 2, as I don't know how to resolve point 1
There are a few things wrong with your attempt that you may not catch right away. One of those issues is the
'_'
used in'%_[0-9]+_%'
is a wildcard for a single character. The 2nd issue is that15
, is not a number between 0 and 9, in which you have a single character search for a number[0-9]
.Maybe not the cleanest, but it works: