简体   繁体   中英

SQL - Cut off at 6th decimal place

So, I have a value 123.123456789, and I'm trying to return 123.123456. The query I tried below rounds the 6th decimal digit to the next number. Please help to return 123.123456 ONLY. (No rounding!)

select cast(cast(123.123456789 as DECIMAL(9,6)) as float)

Use ROUND . The first param is your number, second is the precision, and the third is a number that indicates if the rounding should truncate (0 rounds, any other number truncates).

select cast(cast(round(123.123456789,6,1) as DECIMAL(9,6)) as float)

select round(numeric_expression, length [, function])

Arguments

numeric_expression Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.

length Is the precision to which numeric_expression is to be rounded. length must be an expression of type tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal positions 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.

function Is 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.

as explained here: https://docs.microsoft.com/en-us/sql/t-sql/functions/round-transact-sql?view=sql-server-2017

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM