これがあなたが望むものだと思います:
サンプルデータ:
DECLARE @table TABLE ( barcode VARCHAR(22) )
INSERT INTO @table
(
barcode
)
SELECT '0012145454545654521523'
UNION ALL
SELECT '0012142454545654521523'
UNION ALL
SELECT '5494627448634842135782'
UNION ALL
SELECT '5494625448634842135782'
最初の条件 - 7,8 + 20 を満たす
SELECT a.barcode,
b.barcode,
SUBSTRING(a.barcode, 7, 2) a,
SUBSTRING(b.barcode, 7, 2) b
FROM @table a
INNER JOIN @table b
ON SUBSTRING(a.barcode, 7, 2) + 20 = SUBSTRING(b.barcode, 7, 2)
AND a.barcode != b.barcode
戻り値:
barcode barcode a b
0012145454545654521523 5494627448634842135782 54 74
5494625448634842135782 5494627448634842135782 54 74
7,8 + 20 が存在しない場合の否定
SELECT *
FROM @table a
WHERE NOT EXISTS ( SELECT TOP 1 1
FROM @table b
WHERE SUBSTRING(a.barcode, 7, 2) + 20 = SUBSTRING(b.barcode, 7, 2) )
戻り値:
0012142454545654521523
5494627448634842135782