1. 程式人生 > >SQL數字型別轉換和四捨五入

SQL數字型別轉換和四捨五入

cast和convert用於資料型別轉換,round用於四捨五入取近似字,numeric用於精確設定數字精度(長度)和小數位數。

T-SQL:

CAST and CONVERT

Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality.

Syntax

Using CAST:

CAST ( expressionAS data_type)

Using CONVERT:

CONVERT ( data_type [ ( length) ] ,

expression [ ,style ] )

Arguments

expression

Is any valid Microsoft® SQL Server™ expression. For more information, seeExpressions.

data_type

Is the target system-supplied data type, including bigint andsql_variant. User-defined data types cannot be used. For more information about available data types, see

Data Types.

length

Is an optional parameter of nchar, nvarchar,char, varchar, binary, or varbinary data types.

style

Is the style of date format used to convert datetime orsmalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, ornvarchar data types), or the string format when converting float

, real, money, or smallmoney data to character data (nchar,nvarchar, char, varchar, nchar, or nvarchar data types).

ROUND( ) Function

Returns a numeric expression rounded to a specified number of decimal places.

ROUND(nExpression, nDecimalPlaces)
Return Values

Numeric

Parameters
nExpression
Specifies the numeric expression whose value is to be rounded.
nDecimalPlaces
Specifies the number of decimal places nExpression is rounded to.

If nDecimalPlaces is negative, ROUND( ) returns a whole number containing zeros equal in number tonDecimalPlaces to the left of the decimal point. For example, if nDecimalPlaces is –2, the first and second digits to the left of the decimal point in the value are 0.

Remarks

The value ROUND( ) returns has the same number of decimal places as nDecimalPlaces. ROUND( ) ignores the number of decimal places specified by SET DECIMALS.

Example
SET DECIMALS TO 4
SET FIXED ON     && Fix decimal display
CLEAR

? ROUND(1234.1962, 3) && Displays 1234.1960
? ROUND(1234.1962, 2) && Displays 1234.2000
? ROUND(1234.1962, 0) && Displays 1234.0000
? ROUND(1234.1962, -1)  && Displays 1230.0000
? ROUND(1234.1962, -2)  && Displays 1200.0000
? ROUND(1234.1962, -3)  && Displays 1000.0000

numeric(p,s)

  • p

    Specifies the precision, or the number of digits the object can hold.

  • s

    Specifies the scale, or the number of digits that can be placed to the right of the decimal point.

    p and s must observe the rule: 0 <= s <=p <= 38.

The default maximum precision of numeric and decimal data types is 38. In Transact-SQL,numeric is functionally equivalent to the decimal data type.

Use the decimal data type to store numbers with decimals when the data values must be stored exactly as specified.

將numeric轉換為float或real會導致精度的降低。從 int、smallint、tinyint、float、real、money或smallmoney轉換為decimal或numeric 會導致溢位。將數字轉換為較低精度和小數位數的numeric值時,SQL Server會進行舍入。但如果SET ARITHABORT選項為ON,則發生溢位時,SQL Server會產生錯誤。 若僅降低精度和小數位數,則不會產生錯誤。  
在將float值或實數值轉換為numeric型別時,numeric值不會超過 17 位小數。 任何小於5E-18的float 值總是會轉換為0。

eg:

select cast(round(88.123456789,2) as numeric(5,3)) as TEST
TEST=88.120
select cast(round(88.123456789,4) as numeric(5,3)) as TEST
TEST=88.124,round保留4位小數故將小數第五位的5四捨五入。
select cast(round(88.123456789,4) as numeric(5,4)) as TEST
得不到TEST,err:將 numeric 轉換為資料型別 numeric 時發生算術溢位錯誤。因為進度為5,而且要求保留4位小數,實際是6位,所以精度至少設為6。
select cast(round(88.123456789,4) as numeric(8,6)) as TEST
TEST=88.123500