title: How to get the parent given a child in SQL SERVER 2005
body: <p>I have a table like this</p>
  
  <pre><code>childid      parentid
  ------------------------
  1       0
  2       1
  3       2
  4       2
  5       3
  6       4
  7       0
  8       7
  9       8
  10      1
  </code></pre>
  
  <p>If I give a childid as 5, the parentid will be 1(output)</p>
  
  <p>If I give a childid as 9, the parentid will be 7.(output)</p>
  
  <p>i.e. the root parentid is 0 and the query should stop there.</p>
  
  <p>How to solve such a query?</p>
  
  <p>Please help.</p>