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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:00+00:00 2026-06-13T23:08:00+00:00

I have a column SortIndex in a table, and I would like to add

  • 0

I have a column “SortIndex” in a table, and I would like to add a “case update” to reorder that table by passing in ID and the new position of the chosen one.

SQL Fiddle

DECLARE @T TABLE
(
    ID int,
    Name char(1),
    SortIndex int
)

INSERT @T 
SELECT 21, 'A', 1 UNION ALL
SELECT 23, 'B', 2 UNION ALL
SELECT 35, 'C', 3 UNION ALL
SELECT 45, 'D', 4 UNION ALL
SELECT 55, 'E', 5

SELECT * FROM @T    

DECLARE @SortIndex int

/* MOVE A -> 4 */

SET @SortIndex = (SELECT SortIndex FROM @T WHERE ID = 23)

/*
UPDATE @T
    SET 
        SortIndex = CASE ... ??
*/      

/* MOVE D -> 2 */

SELECT * FROM @T

Is this possible to solve this by only using the case statement to rebuild the sortindex’es?

UPDATE

Desired results

A -> 4

    Name SortIndex
    B     1 
    C     2
    D     3
    A     4
    E     5

    D -> 2

    A     1 
    D     2
    B     3
    C     4
    E     5
  • 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-13T23:08:00+00:00Added an answer on June 13, 2026 at 11:08 pm

    Use following code to move and rebuild indexes, it adds element after desired elements and shifts others (answer based on the comment clarifications ) :

    This is slimmer version:

     DECLARE @SortIndex int,@MoveTo int, @CurrentSortIndex int
    
    SET @CurrentSortIndex  = (SELECT SortIndex FROM @T WHERE ID = 45) --D
    SET @MoveTo = 2
    
    UPDATE--D
      @T
    SET
      SortIndex = @MoveTo
    WHERE
      ID = 45
    
    UPDATE
      @T
    SET
      SortIndex = SortIndex - ((@MoveTo-@CurrentSortIndex)/(ABS(@MoveTo-@CurrentSortIndex)))
    WHERE
    (
        SortIndex BETWEEN @CurrentSortIndex AND @MoveTo
     OR
        SortIndex BETWEEN @MoveTo AND @CurrentSortIndex  
    )
    AND
      ID != 45
    

    Full example of moving A to position 2, also sqlfidle link http://sqlfiddle.com/#!3/d41d8/5918:

    DECLARE @T TABLE
    (
        ID int,
        Name char(1),
        SortIndex int
    )
    
    INSERT @T 
    SELECT 21, 'A', 1 UNION ALL
    SELECT 23, 'B', 2 UNION ALL
    SELECT 35, 'C', 3 UNION ALL
    SELECT 45, 'D', 4 UNION ALL
    SELECT 55, 'E', 5
    
    
    DECLARE @SortIndex int,@MoveTo int, @CurrentSortIndex int
    DECLARE @IDToMove INT 
    SET @IDToMove = 21--A
    
    SET @CurrentSortIndex  = (SELECT SortIndex FROM @T WHERE ID = @IDToMove) 
    SET @MoveTo = 2
    
    UPDATE
      @T
    SET
      SortIndex = @MoveTo
    WHERE
      ID = @IDToMove
    
    UPDATE
      @T
    SET
      SortIndex = SortIndex - ((@MoveTo-@CurrentSortIndex)/(ABS(@MoveTo-@CurrentSortIndex)))
    WHERE
    (
        SortIndex BETWEEN @CurrentSortIndex AND @MoveTo
     OR
        SortIndex BETWEEN @MoveTo AND @CurrentSortIndex  
    )
    AND
      ID != @IDToMove
    
    SELECT * FROM @T ORDER BY SortIndex
    

    Initial version:

    DECLARE @SortIndex int,@MoveTo int, @CurrentSortIndex int
    
    SET @CurrentSortIndex  = (SELECT SortIndex FROM @T WHERE ID = 21) --A
    SET @MoveTo = 2
    
    UPDATE--A
      @T
    SET
      SortIndex = @MoveTo
    WHERE
      ID = 21
    
    IF @MoveTo > @CurrentSortIndex
    
    UPDATE
      @T
    SET
      SortIndex = SortIndex - 1
    WHERE
      SortIndex BETWEEN @CurrentSortIndex AND @MoveTo
    AND
      ID != 21
    
     IF @MoveTo < @CurrentSortIndex
    
    UPDATE
      @T
    SET
      SortIndex = SortIndex + 1
    WHERE
      SortIndex BETWEEN @MoveTo AND @CurrentSortIndex
    AND
      ID != 21
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql table that I am trying to add a column from
I have column with varchar values like 022008 that I need to convert into
I have column with the type ntext , and I would like to convert
For example, i have column tables pallet_id and serial I would like to query
I would like to perform query in which as a result I have column
I have column in my table, say updateStamp . I'd like to get an
I have column that contains strings. The strings in that column look like this:
I have a single table containing many users. In that table I have column
I need a select from table which does not have column that tells when
I have column in a table that allow null but i want to make

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.