title: SQL query to get immediate parent which satisfies a condition
body: <p>I have a category table where hierarchy is maintained by id, parentid (i.e. one category's ID as parentid of another category then it's a child of that element). I want to recursively go up from a child to immediate parent and check one of the column say FooterAdId is null or not. If null go upwards and continue checking else return the FooterAdId. I'm able to get the top parent with a cte recursive query but confused how I apply the condition to the above described scenario...</p>
  
  <p><img src="https://i.stack.imgur.com/Ms1Rn.jpg" alt="enter image description here"></p>
  
  <p>Here I have id 6 in my hand and the result I want as described above will be 101.</p>