Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7078177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:30:52+00:00 2026-05-28T06:30:52+00:00

I am looking for a SQL Server function that I can supply 3 primary

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:30:53+00:00Added an answer on May 28, 2026 at 6:30 am

    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.

    CREATE FUNCTION dbo.fnCompareSoftware
    --Provide the function with the minID and maxID and the testID and it will determine if the testID is between the minID and maxID
    (
     @minID int,
     @maxID int,
     @testID int
    )
    RETURNS bit
    AS
    BEGIN
    DECLARE @rowTestNumber int
    --inclusiveness
    if @minID = @testID
      BEGIN
      RETURN 1
      END
    if @maxID = @testID
      BEGIN
      RETURN 1
      END
    
    SELECT @rowTestNumber = rowNumber FROM (
      --SELECT the 3 softwareID, then order them properly, then give them row numbers. Based on the results we can determine the result.
      SELECT
      ROW_NUMBER() OVER (ORDER BY Version1,Version2,Version3,Version4,Version5 ASC) AS rowNumber, Software.ID,Text
      FROM Software
      WHERE ID IN(@minID,@maxID,@testID)
    ) as Results
    WHERE ID = @testID
    
    --If minID is 0, then we are checking if the @testID is less than the @maxID. If so, @maxID will have the number 2 spot, and @testID will be #1
    if @minID = 0 AND @maxID !=0 AND @rowTestNumber = 1
      BEGIN
      RETURN 1
      END
    --If maxID is 0, then we are checking if the @testID is more than the @minID. If so, @minID will have the number 1 spot, and @testID will be #2
    if @minID != 0 AND @maxID =0 AND @rowTestNumber = 2
      BEGIN
      RETURN 1
      END
    if @minID != 0 AND @maxID !=0 AND @rowTestNumber = 2
      BEGIN
      RETURN 1
      END
    --else we return 0  
    RETURN 0
    END
    GO
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an Access 2007 equivalent to SQL Server's COALESCE function. In SQL
I am looking at a large-ish SQL Server 2005 database, that has entered into
I made the following function in SQL Server 2008 earlier this week that takes
I am looking for something that works in SQL Server similar to the @
The ident_Current function is returning a null on sql server 2005. After looking at
I am getting deadlocks occasionally in sql server. I created a function for locking
I am looking for SQL Server Profiler in SQL Server 2008 express edition.I have
I'm looking for SQL Server studio management-like and redgate sqlcompare/datacompare-like tools for MySQL. What
I'm looking to implement SQL Server Reporting Services as our standard reporting platform in
I am looking to manage a SQL Server 2008 DB using Management Studio 2005.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.