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 8243321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:28:41+00:00 2026-06-07T21:28:41+00:00

I have a column in a SQL Server database table, there is a column

  • 0

I have a column in a SQL Server database table, there is a column containing integer values and I would like to get the max value unless there is a gap in the series, then I would like to get the first missing value.

For example from the below table, I am looking to get value 4

1
2
null
3

The above is simple, however if the table contains data like below, how can I find which id I am missing, in this case it would be 8

1
3
2
4
null
5
7
6
null
10
9

//////////////////

Edit:

I Initially implemented @podiluska’s answer and was going to accept it however when I tested with a table with over 10K rows it was quite slow, so I have taken @vmvadivel’s answer and changed it slightly as shown below and it works quite fast, but still not 100% what I wanted:

SELECT Number
FROM 
(
    SELECT ROW_NUMBER() OVER(ORDER BY iValue) AS Number
    FROM tblNumbers WHERE iValue IS NOT NULL
) temp
WHERE Number NOT IN 
(
    SELECT iValue FROM tblNumbers WHERE iValue IS NOT NULL
)

This works quite well however if all the iValue fields are null then I don’t get anything back, but I want it to return 1 as it will be the first value in series, obviously I can handle this in the C# code but I am sure there must be a way of returning 1 if the above query does not return a row?

I have changed the top level Select Number with SELECT Coalesce(Number, 1) but it did not work, any ideas how to solve this?

Thanks

  • 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-06-07T21:28:43+00:00Added an answer on June 7, 2026 at 9:28 pm

    If #t is your table

    select min(t1.number)+1 
    from #t t1
    left join #t t2 
               on t1.number = t2.number-1
    where t2.number is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL Server 2008 database with a table that has a column
I have a Products table in SQL Server database, it has an ID column
I have a database in SQL Server containing a column which needs to contain
I have a database table that uses Sql Server 2005's uniqueidentifier column. I know
I have a SQL Server 2005 database. Table A has an identity column in
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a column type money in my sql server database, which is moneyAcumulated
I have a SQL Server 2000 database that will not display the column list
I have a SQL Server 2000 database with a column of type VARCHAR(255). All
I have an SQL Server 2008 database with a column of type geography storing

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.