title: Some rows of Order by column is null. How to make Order by column optional on that situation
body: <p>I have a prob in following  Select query.</p>
  
  <pre><code>select * 
  from ".$table2." a,purpose_details b 
  where  b.purpose_code=a.purpose and purpose_code in(1,4,6,7,10)  
    and ((fromdt&lt;='$frmdate'and todt&gt;='$frmdate') 
           or (fromdt&lt;='$frmdate' and todt='1111-11-11')) 
    and  substr(a.appno,4,1)!=6  
  order by purpose_priority,
           cast(substr(a.case_no,12,4) as int) ,cast(substr(a.case_no,4,1) as int),
           cast(substr(a.case_no,5,7) as int),cast(substr(a.appno,12,4) as int) ,
           cast(substr(a.appno,4,1) as int),cast(substr(a.appno,5,7) as int)";
  </code></pre>
  
  <p>the problem is the rows in the table2 may or may not have value of appno. (ie) appno value may be NULL for some of rows.</p>
  
  <p>Due to appno is one among in the order by column this particular code retuns invalid input error.</p>
  
  <p>Finally What i want is the <strong>select query arrange the columns by appno only when the appno value is not null</strong>.</p>
  
  <p>Note: The order by clause for the rest of the columns should be applied on both situations.</p>
  
  <p>Pls help me to sort it out. Thanks in Advance.</p>