I’m looking for a query to get the following output:
Id Number
-- ------
1 241100
2 241110
2 241111
2 241112
2 241113
2 241114
2 241115
Table strucutre:
Id Number From To
-- ------ ---- ----
1 241100 NULL NULL
2 241110 111 115
Rows without a from/to range has to return the number. The other ones have to return the number followed by SUBSTRING(Number, 1, 3) + <from/to range>
One possible solution would be using while-loops. But thats not the way I’d prefer. And it’s quite slow. And there’s no way to change the data structure. We query data from a third party supplier.
On application site I’ve a (very small) list of numbers such as ‘241113’, ‘241000’, … and need to know to which id this number is assigned to.
The query I will use to get a result is:
SELECT Id, Number FROM MyView WHERE Number IN ('241113', '241000')
This is how you can write that query instead. There is no need to generate the numbers.
A version where you put the numbers you look for in a table variable instead.
I have not used
Fromanywhere because it is unclear to me what values is possible in that column other thennullandnumber+1.And a version where you generate the numbers before filtering them out. The result is the same and I believe performance is not as good.