Not the most generic of methods, but if you knew that the largest number you are looking for is 999 then you only have to check the last 3 chars and then sub-string them out.
select X.INBOX_COL
-- Substring out the number component
-- Cheeky way to ignore dash ('-')
, replace(substring(X.INBOX_COL, Z.StringLength-Z.NumberLength+1, Z.NumberLength),'-','') Quantity
from (
values
('14AF08'),
('14AF09'),
('17NAMX14'),
('17NAMX16'),
('21KMTZ7'),
('21KMTZ8'),
('21KMTZ9'),
('B-09'),
('B-14')
) X (INBOX_COL)
cross apply (
-- Assume max value number is 999, so check last 3 characters of string
values (
isnumeric(case when len(X.INBOX_COL) > 0 then substring(reverse(X.INBOX_COL),1,1) else 'z' end)
, isnumeric(case when len(X.INBOX_COL) > 1 then substring(reverse(X.INBOX_COL),2,1) else 'z' end)
, isnumeric(case when len(X.INBOX_COL) > 2 then substring(reverse(X.INBOX_COL),3,1) else 'z' end)
)
) Y (One, Two, Three)
cross apply (
-- Get the expected length of the number string (and the length of the actual string)
values (
len(X.INBOX_COL), case when One = 1 and Two = 1 and Three = 1 then 3 when One = 1 and Two = 1 then 2 when One = 1 then 1 else 0 end
)
) Z (StringLength, NumberLength);
Returns:
INBOX_COL |
Quantity |
14AF08 |
08 |
14AF09 |
09 |
17NAMX14 |
14 |
17NAMX16 |
16 |
21KMTZ7 |
7 |
21KMTZ8 |
8 |
21KMTZ9 |
9 |
B-09 |
09 |
B-14 |
14 |