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

  • SEARCH
  • Home
  • 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 8140483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:10:55+00:00 2026-06-06T12:10:55+00:00

I have a table with a column where the values should not be the

  • 0

I have a table with a column where the values should not be the same. Due to poor implementation software does not check to ensure if user enters a duplicate or not, therefore I was entrusted with a task of writing a “simple” sql statement or function that would look at all the values in the column and ensure they are not identical. The sql itself will be executed once a month so it does not have to be efficient.

The column stores int and I would like to increment one of the duplicate values, and keep on doing it every time until there are no more entries in the table that have the same value for the column. Any suggestion what I should do? I am not sure where to even start.

Thanks to anyone for their insight.

EDIT: Sorry forgot to mention that the values in the data that are not duplicate should stay that way. These are the settings used for customers, and we should punish them only if they have duplicate values. If they do not, then we should not destroy their settings or they will be angry.

It is just one table, contains a lot of columns, but this one specific column in particular which is of type int and never contains any nulls, should not have any duplicates.

  • 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-06T12:10:56+00:00Added an answer on June 6, 2026 at 12:10 pm
     create table tableName (KeyCol int identity(1,1), intCol int)
     insert into tableName values (1), (2), (2), (3), (5), (6), (5), (7), (9)
    
    
    While exists (
        select intCol
        from tableName
        group by intCol
        having count(*) > 1
    )
    begin
    
        declare @newValue int
    
        select @newValue = min(t1.intCol) + 1
        from tableName t1
        left join tableName t2
        on t1.intCol + 1 = t2.intCol
        where t2.intCol is null
    
        update tx
        set intCol = @newValue
        from tableName tx
        where intCol = 
        (select top 1 intCol
        from tableName t1
        group by intCol
        having count(*) > 1
        order by intCol)
        and KeyCol = (select min(KeyCol) from tableName t2 where t2.intCol = tx.intCol)
    end
    
    select * from tableName order by 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a table where the cells in a column should not have
I have a table with a column whose values come from an Enumeration. I
If I have table with a Date column called myDate , with values like
Is it possible to edit computed-column values? I have a computed-col in my table,
I have a table of many values where one column has the WO Number,
i have a large mysql database table in which one column contains values ranging
In a MySQL-table I have a VARCHAR -column with different values, which may represent
I have a table with a column of bit values. I want to write
I have many tables that use Lookup/Enum references for most of their column values.
I have a value in my MS SQL table column called XDATA like this:

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.