I am looking for a SQL Server function that I can supply 3 primary keys to ex.
functionFoo (@maxID, @minID, @testID)
with the result being 1 (true) if the @testID version is between the @maxID and @minID version values where there are multiple columns with integers that are being compared.
I would also like the function to be able to handle open ended comparison, such that if the @maxID is NULL then it will just check if the @testID columns are greater than the @minID columns. Or if the @minID is NULL then it will check if the @testID columns are less than the @maxID columns.
Take this contrived example of comparing versions for software. Say you have multiple versioned software. For example say I have software1 with a version 1.6.6.978.73 and I now want a SQL Server function to determine if it falls between version 1.5.9.7 and version 1.6.7. My tables would be set up like below
Software Table
ID Version1 Version2 Version3 Version4 Version5
1 1 6 6 978 73
2 1 5 9 7 NULL
3 1 6 7 NULL NULL
Where Version1 column is always weighted higher than the Version2 column (e.g. version 2.0 is higher than 1.99999). I’ve tried to go through each column and do a comparison and if the result is ambiguous then I move to the next column and again perform a comparison, but it seems tedious and difficult to read. Is there is a better/simpler way? Am I missing a built in SQL Server function? It is very similar to sorting something by alphabetical order, with a recursive nature where if the first comparison does not yield a determinate result we go one level deeper.
My approach to this problem was to select the 3 records, and sort them using the ORDER BY clause. Then apply a ROW_NUMBER() to the records and have some simple if statements determine the output of the function. This function only makes sure the @testID is betweeen the @minID and @maxID, thus @maxID could be less then @minID and it shouldn’t make a difference. That was fine for what I needed. I’m not sure it is the best way to do it, but I think it is very readable and the logic makes sense.