English 中文(简体)
从一栏中选取最大回报的马克斯
原标题:Select Max number from column doesnt return max

我正在进行 que问,将一栏中的最大数值退回该方案。

斜线一行。

select MAX([Line No]) from table

I am using sql server 2008.

该表有三行:线 无数值50、90、100

the max value i expect to be returned is 100, but im getting 90..why?

EDIT:

  string LineNo = "0";

   //LineNo
   string SQLlineno = "select MAX(CAST([Line No] As Int)) from Saved_Order_Import where [order no] =  "
                      + ordernumber + " ";
   SqlCommand myCommandlineno = new SqlCommand(SQLlineno, myConnection);
   SqlDataReader readerline;
   try
   {
       readerline = myCommandlineno.ExecuteReader();
       if (readerline.HasRows)
       {
           while (readerline.Read())
           {
               try
               {
                   if (readerline.GetString(0) != null)
                   {
                       LineNo = (readerline.GetString(0) + 10).ToString();
                   }
               }
               catch (Exception ex) { return "{"error":"line : " + ex.ToString() + ""}"; }
           }
       }
       readerline.Close();
   }
   catch (Exception ex)
   {
      // return "{"error":"Other One11" + ex.ToString() + ""}";
   }
最佳回答

你的一栏可能不是数字型,而是显示型(果、 n、var、 n)。 因此,它正在对甲型六氯环己烷进行分类,9个在1个之后。

The easiest thing is to change to the correct data type for that column. If you can t do that, you ll need to cast back to the correct type inside the MAX.

例如,

select max(cast ([Line No] as int)) 
from table 

如果你在该栏中有一些价值不能转化为你想要过滤的数字,你可以做到:

select max(cast ([Line No] as int)) 
from table 
where isnumeric([Line No]) = 1

或者,为了显示非数字价值,你可以确定这些数值,你可以做到:

select *
from table 
where isnumeric([Line No]) = 0

更强有力的惯性检测,见本条:。 I Str to

问题回答
select MAX(cast([Line No] as int)) from table

UPADTE: Call the ExecuteScalar on your SqlCommand not GetSring.

int max = (int)myCommandlineno.ExecuteScalar();

As RedFilter s answer,it may be a different type. Cast the column value to a numeric type and then apply the Max function

select MAX(CAST(LineNo AS INT) from table

为了从文字数据中得出数字最高点,你必须得出以下数值:

select max(cast([Line No] as int)) from table

如果你在一栏中具有其他字母数字价值,不能被转换成不变,而且你想要忽视这些数值,但你仍然希望回到法典中来,你可以这样做:

select cast(max(cast(LineNo AS int) as varchar(10))
from table
where isnumeric([LineNo] = 1)

然而,如果该栏意在打乱,我建议你安排一些发展时间,以改变该栏的类型,并将任何非签署国移至可接受的数值。 如果你不使用适当的数据类型,你会真正陷入困境。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签