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!
string SQL = "select MAX(CONVERT(int, myField)) from myTable where myCode = '" + theCust + "'";
string SQL = "select myField from myTable where myCode = '" + theCust + "'";
You can try it like this:
SELECT TOP 1 CAST(MyColumn AS int) AS TheMax FROM MyTable ORDER BY TheMax DESC
So (using the sloppy method, always paramaterize!)
String sql = "SELECT TOP 1 CAST(MyColumn AS int) AS TheMax FROM MyTable WHERE MyParam = '" + param + "' ORDER BY TheMax Desc"; //Fill DataAdapter/DataReader etc.