You might call the generic abilities of XML to your rescue. Not very fast with may rows, but very mighty:
(credits to iamdave for the mockup)
declare @t table(id int,diag1 int,diag2 int,diag3 int,diag4 int,diag5 int,diag6 int,diag7 int,diag8 int,diag9 int,diag10 int);
insert into @t (id, diag5) values(1,12345);
insert into @t (id, diag3) values(2,123);
insert into @t (id, diag8, diag1) values(3,123,12345);
insert into @t (id, diag9, diag2) values(4,345,678);
WITH CreateXML AS
(
SELECT *
,(
SELECT *
FROM @t t2
WHERE t1.id=t2.id
FOR XML PATH('codes'),TYPE
).query('/codes/*[substring(local-name(),1,4)="diag"]') AllCodes
FROM @t t1
)
SELECT *
FROM CreateXML
WHERE AllCodes.exist('/*[. cast as xs:int? >=12345 and . cast as xs:int? <=12349]')=1;
The query will use a SELECT *
to create an XML of all columns. The .query()
will pick all elements starting with "diag"
. Maybe you have to adapt this to your needs.
The .exist()
will check, if any value within these elements is between your borders. One match is enough.
Good Luck!