title: Select highest number from table when number stored as string?
body: <p>I'm trying to write a windows forms app in C# .Net 4 it connects to a SQL Server 2008 database and I want to Select highest number from a table where the number is stored as string!</p>
  
  <pre><code>string SQL = "select MAX(CONVERT(int, myField)) from myTable where myCode = '" + theCust + "'";
  </code></pre>
  
  <p>I have also tried Max(CAST(myField as Int)) in the select statement but both fail to return anything even though the Database has for the theCust two rows with 10001 and 10002. The Error i Get is "Enumeration yielded no results"</p>
  
  <p>What am I doing wrong?</p>
  
  <p>I'm using the in built System.Data.SqlClient and if I just do a </p>
  
  <pre><code>string SQL = "select myField from myTable where myCode = '" + theCust + "'";
  </code></pre>
  
  <p>it returns both numbers as strings. I know I could sort them in code but if the Database gets large that would not be a good approach!</p>
  
  <p>I just tried it again with an int Field in the db and still got the same error! Is Max the wrong thing to be using?</p>