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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:55:36+00:00 2026-05-12T09:55:36+00:00

Using Hierarchy data type on SQL 2008. Nodes in my hierarchy go like this:

  • 0

Using Hierarchy data type on SQL 2008. Nodes in my hierarchy go like this:

value   node 
36    /8/1/
38    /8/2/
34    /8/3/
40    /8/4/
42    /8/5/
44    /8/6/
46    /8/7/
48    /8/8/

I’d like to rearrange nodes so that /8/3/ and /8/1/ switch places. Any idea on how to do this?

Only idea that I have so far is that I load all nodes on a level in Array, order them the way I want them to be, delete them from table and insert in sorted form.

  • 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-12T09:55:36+00:00Added an answer on May 12, 2026 at 9:55 am

    If (like in your example) you know the hierarchyid values you want to manipulate in advance, you can do it directly, e.g.:

    -- Place 1st node between 2nd and 3rd
    UPDATE yourTable SET node = CAST('/8/2.5/' AS hierarchyid) WHERE value = 36;
    -- Move 3rd node to 1st
    UPDATE yourTable SET node = CAST('/8/1/' AS hierarchyid) WHERE value = 34;
    

    If you need to get your new hierarchyid values dynamically, take a look into the GetDescendant() and GetAncestor() functions. Using that, the example would look something like this:

    DECLARE @Parent hierarchyid, @Child1 hierarchyid, @Child2 hierarchyid
    
    -- Grab hierarchyids from 2nd and 3rd node
    SELECT @Child1 = node FROM yourTable WHERE value = 38;
    SELECT @Child2 = node FROM yourTable WHERE value = 34;
    -- Get the parent hierarchyid
    SELECT @Parent = @Child1.GetAncestor(1);
    
    -- Update 1st node to end up between 2nd and 3rd
    UPDATE yourTable SET node = @Parent.GetDescendant(@Child1, @Child2) WHERE value = 36;
    -- Update 3rd node to end up before 2nd
    UPDATE yourTable SET node = @Parent.GetDescendant(NULL, @Child1) WHERE value = 34;
    

    Note that the hierarchyids do not stay the same in this example. At least one will end up using a ‘fraction’ for its position (e.g. ‘/8/2.5/’ instead of ‘/8/3/’).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 217k
  • Answers 217k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe something like this: UITabBar *tabBar = [[UITabBar alloc] init];… May 12, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer Short of faking your own scrollbar with JavaScript, it is… May 12, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer Tests are not supposed to run during import. Maybe you… May 12, 2026 at 11:19 pm

Related Questions

I'm working on a RoR app, but this is a general question of strategy
I am interested in your opinions on unit-testing code that uses Corba to communicate
We are in evaluating technologies that we'll use to store data that we gather
I am writing a library that I would like to be portable. Thus, it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.