Always keep decimals, calculation in the program is too cumbersome, but also to convert the operation. It is convenient to operate directly in the database.
The data type is defined as Decimal/numeric type, the decimal place needs to be arbitrarily set, divisor and dividend must have a type in decimal/numeric,
The following example:
SELECT 24.0000/38.0000--0
SELECT CAST (24/38 as NUMERIC (18,4))--0
SELECT CAST (24/38.0000 as NUMERIC (18,4))--0.6316 This result has been rounded
SELECT 24.0000/38.0000--0.6316
Decimal/numeric and int type operations result in the Decimal/numeric type because the decimal/numeric has a higher precedence than int.
Refer to the Type Priority table on MSDN:
SQL Server 2005 uses the following order of precedence for data types:
- User-defined data type (highest)
- sql_variant
- XML
- datetime
- smalldatetime
- float
- Real
- decimal
- Money
- smallmoney
- bigint
- int
- smallint
- tinyint
- bit
- ntext
- text
- Image
- timestamp
- uniqueidentifier
- nvarchar(including nvarchar (max))
- nchar
- varchar (including varchar (max))
- Char
- varbinary(including varbinary (max))
- binary(lowest)
Calculation results in SQL Server retain decimals