strike
Lv 5
strike 發問時間: 電腦與網際網路程式設計 · 1 0 年前

sql server會自己作無條件捨去

我的資料庫有兩個資料表

分別是A及B資料表

當A資料表的A欄位除以B資料表的B欄位時

由於此兩個欄位

都是用integer

結果下 sql語法作合計sum時

它會自動無條件捨去

但我想要求到小數位第二位作四捨五入

請問要如何解決呢?

謝謝

1 個解答

評分
  • 振煒
    Lv 5
    1 0 年前
    最佳解答

    你可以試著使用下列語法:select round(((a+0.0) / (b+0.0)),2) from tablename------------------------------------------------------------ROUND-的使用方法Returns a numeric expression, rounded to the specified length or precision.SyntaxROUND ( numeric_expression , length [ , function ] ) Argumentsnumeric_expressionIs an expression of the exact numeric or approximate numeric data type category, except for the bit data type.lengthIs the precision to which numeric_expression is to be rounded. length must be tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal places specified by length. When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.functionIs the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.Return TypesReturns the same type as numeric_expression------------------------------------------------------------------+0.0的主要是通知編譯器不要轉成int作運算。

    參考資料: 自已的經驗
還有問題?馬上發問,尋求解答。