Не уверен, правильно ли я перехватил вашу логику, но есть вариант, когда код в OUTER APPLY
может быть преобразован в Табличную функцию
Пример dbFiddle
Declare @YourTable table (ID varchar(13))
Insert Into @YourTable values
('0002131000008'),('9910260000006'),('9907089000001'),('9903208000006'),('9811307000001'), ('9808126000006'),('null'),('9905292000002'),('9702081000004'),('9610210000003'),('9607039000008'),('9603158000002'), ('9511267000005'),('9504205000005'),('9412314000009'),('9409123000006'),('9405252000007'),('9310170000008'),('9306299000002'), ('9303118000008'),('9211217000003'),('9208036000008'),('9204155000002'),('9112274000003'),('9105212000002'),('9101311000001'), ('9010130000003'),('9006259000006'),('9003078000002'),('8911177000007'),('8907306000009'),('8904115000007'),('8809033000007'), ('8805162000008'),('8801271000005'),('8710090000007'),('8706219000001'),('8703038000007'),('8611137000002'),('8607266000003'), ('8604075000001'),('8512184000004'),('8508303000008'),('8505122000003'),('8501221000002'),('8413040000005'),('8406169000008'), ('8402278000005'),('8311097000007'),('8307226000009'),('8304035000007'),('8208263000003'),('8205082000008'),('8201181000007'), ('8109300000008'),('8106129000003'),('8102228000002'),('8011047000004'),('8007176000005'),('7912104000004'),('7908223000008'), ('7905042000003'),('7901141000002'),('7809260000002'),('7806089000007'),('7802188000006'),('7710317000005'),('7703255000004'), ('7608173000004'),('7604292000009'),('7601101000006'),('7509220000007'),('7506049000000'),('7502148000001'),('7407096000005'), ('7312014000006'),('7304252000004'),('7301051000003'),('7209170000004'),('7205309000006'),('7202108000006'),('7110237000005'), ('7107056000009'),('7103175000004')
Select A.ID
,B.*
From @YourTable A
Outer Apply (
Select IsValid = case when sum(v) % 10 = 1 then 'Valid' else 'Not Valid' end
from (
Select p
,v = v+iif(v*2>9 and p in (12,10,8,6,4,2) ,-9,0)
From ( values (1 ,0+substring(ID, 1,1))
,(2 ,0+substring(ID, 2,1))
,(3 ,0+substring(ID, 3,1))
,(4 ,0+substring(ID, 4,1))
,(5 ,0+substring(ID, 5,1))
,(6 ,0+substring(ID, 6,1))
,(7 ,0+substring(ID, 7,1))
,(8 ,0+substring(ID, 8,1))
,(9 ,0+substring(ID, 9,1))
,(10,0+substring(ID,10,1))
,(11,0+substring(ID,11,1))
,(12,0+substring(ID,12,1))
,(13,0+substring(ID,13,1))
) a(p,v)
Where try_convert(bigint,ID) is not null
) a
) B
Возвращает
ID IsValid
0002131000008 Not Valid
9910260000006 Not Valid
9907089000001 Not Valid
9903208000006 Not Valid
9811307000001 Valid
9808126000006 Not Valid
null Not Valid
9905292000002 Valid
9702081000004 Not Valid
9610210000003 Not Valid
9607039000008 Not Valid
9603158000002 Not Valid
9511267000005 Not Valid
9504205000005 Valid
9412314000009 Not Valid
9409123000006 Not Valid
9405252000007 Not Valid
9310170000008 Not Valid
9306299000002 Not Valid
9303118000008 Not Valid
9211217000003 Not Valid
9208036000008 Not Valid
9204155000002 Not Valid
9112274000003 Not Valid
9105212000002 Not Valid
9101311000001 Not Valid
9010130000003 Not Valid
9006259000006 Not Valid
9003078000002 Not Valid
8911177000007 Not Valid
8907306000009 Not Valid
8904115000007 Not Valid
8809033000007 Not Valid
8805162000008 Valid
8801271000005 Not Valid
8710090000007 Not Valid
8706219000001 Not Valid
8703038000007 Not Valid
8611137000002 Not Valid
8607266000003 Valid
8604075000001 Not Valid
8512184000004 Not Valid
8508303000008 Not Valid
8505122000003 Not Valid
8501221000002 Not Valid
8413040000005 Not Valid
8406169000008 Not Valid
8402278000005 Not Valid
8311097000007 Not Valid
8307226000009 Not Valid
8304035000007 Not Valid
8208263000003 Not Valid
8205082000008 Not Valid
8201181000007 Not Valid
8109300000008 Not Valid
8106129000003 Valid
8102228000002 Not Valid
8011047000004 Not Valid
8007176000005 Not Valid
7912104000004 Not Valid
7908223000008 Valid
7905042000003 Not Valid
7901141000002 Not Valid
7809260000002 Not Valid
7806089000007 Not Valid
7802188000006 Not Valid
7710317000005 Not Valid
7703255000004 Not Valid
7608173000004 Not Valid
7604292000009 Valid
7601101000006 Not Valid
7509220000007 Not Valid
7506049000000 Not Valid
7502148000001 Not Valid
7407096000005 Not Valid
7312014000006 Not Valid
7304252000004 Not Valid
7301051000003 Valid
7209170000004 Not Valid
7205309000006 Not Valid
7202108000006 Not Valid
7110237000005 Not Valid
7107056000009 Not Valid
7103175000004 Not Valid