Programing

SQL Server에서 소수점 이하 자르기 (Round 아님)

lottogame 2020. 5. 17. 10:29
반응형

SQL Server에서 소수점 이하 자르기 (Round 아님)


반올림하지 않고 SQL에서 소수점 이하 자릿수를 자르거나 삭제하는 가장 좋은 방법을 결정하려고합니다. 예를 들면 다음과 같습니다.

declare @value decimal(18,2)

set @value = 123.456

이것은 자동으로 라운드 할 @value수하는 123.46대부분의 경우 좋은이다. 그러나이 프로젝트에는 필요하지 않습니다. 필요하지 않은 소수를 자르는 간단한 방법이 있습니까? left()함수를 사용하고 10 진수로 다시 변환 할 수 있다는 것을 알고 있습니다 . 다른 방법이 있습니까?


select round(123.456, 2, 1)

ROUND ( 123.456 , 2 , 1 )

세 번째 매개 변수 ! = 0 일 때 반올림하지 않고 잘립니다.

http://msdn.microsoft.com/en-us/library/ms175003(SQL.90).aspx

통사론

ROUND ( numeric_expression , length [ ,function ] )

인수

  • numeric_expression 비트 데이터 유형을 제외한 정확한 숫자 또는 대략적인 숫자 데이터 유형 범주의 표현식입니다.

  • lengthnumeric_expression을 반올림 할 정밀도입니다. length는 tinyint, smallint 또는 int 유형의 표현식이어야합니다. length가 양수인 경우 numeric_expression은 length로 지정된 소수 자릿수로 반올림됩니다. length가 음수이면 numeric_expression은 length로 지정된 소수점 왼쪽에서 반올림됩니다.

  • function수행 할 작업 유형입니다. 함수는 tinyint, smallint 또는 int 여야합니다. 함수가 생략되거나 값이 0 (기본값)이면 numeric_expression이 반올림됩니다. 0 이외의 값을 지정하면 numeric_expression이 잘립니다.

SELECT Cast(Round(123.456,2,1) as decimal(18,2))

자르고 둥글게하지 않는 방법은 다음과 같습니다.

select 100.0019-(100.0019%.001)

100.0010을 반환

그리고 당신의 예 :

select 123.456-(123.456%.001)

123.450을 반환

이제 끝나는 0을 제거하려면 간단히 캐스트하십시오.

select cast((123.456-(123.456%.001)) as decimal (18,2))

123.45를 반환


실제로 세 번째 매개 변수가 0 또는 1 또는 2이면 값을 반올림하지 않습니다.

CAST(ROUND(10.0055,2,0) AS NUMERIC(10,2))

라운드는 선택적 매개 변수가 있습니다

Select round(123.456, 2, 1)  will = 123.45
Select round(123.456, 2, 0)  will = 123.46

반올림 솔루션과 예제가없는 다른 자르기.

    Convert 71.950005666 to a single decimal place number (71.9)
    1) 71.950005666 * 10.0 = 719.50005666
    2) Floor(719.50005666) = 719.0
    3) 719.0 / 10.0 = 71.9

    select Floor(71.950005666 * 10.0) / 10.0

소수점을 원하십니까?

그렇지 않은 경우

select ceiling(@value),floor(@value)

0으로하면 라운드를 수행하십시오.

select round(@value,2)

이것은 모든 숫자의 소수 부분을 제거합니다

SELECT ROUND(@val,0,1)

나는 이것이 꽤 늦었다는 것을 알고 있지만 대답으로 보지 못하고 수년간이 트릭을 사용 해 왔습니다.

단순히 값에서 .005를 빼고 Round (@ num, 2)를 사용하십시오.

귀하의 예 :

declare @num decimal(9,5) = 123.456

select round(@num-.005,2)

123.45를 반환

원하는 올바른 값으로 반올림을 자동으로 조정합니다.

그건 그렇고, 영화 Office Space에서 프로그램을 다시 작성하고 있습니까?


점 다음에 소수점 3 자리를 소수점 2 자리로 변환하려면이 코드를 사용하십시오.

declare @val decimal (8, 2)
select @val = 123.456
select @val =  @val

select @val

출력은 123.46입니다


나는 당신이 10 진수 값만을 ​​원한다고 생각합니다.이 경우 다음을 사용할 수 있습니다 :

declare @val decimal (8, 3)
SET @val = 123.456

SELECT @val - ROUND(@val,0,1)

Another way is ODBC TRUNCATE function:

DECLARE @value DECIMAL(18,3) =123.456;

SELECT @value AS val, {fn TRUNCATE(@value, 2)} AS result

LiveDemo

Output:

╔═════════╦═════════╗
║   val   ║ result  ║
╠═════════╬═════════╣
║ 123,456 ║ 123,450 ║
╚═════════╩═════════╝

Remark:

I recommend using built-in ROUND function with 3rd parameter set to 1.


I know this question is really old but nobody used sub-strings to round. This as advantage the ability to round really long numbers (limit of your string in SQL server which is usually 8000 characters):

SUBSTRING('123.456', 1, CHARINDEX('.', '123.456') + 2)

I think we can go much easier with simpler example solution found in Hackerrank:

Problem statement: Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345. Truncate your answer to 4 decimal places.

SELECT TRUNCATE(MAX(LAT_N),4)
FROM STATION
WHERE LAT_N < 137.23453;

Solution Above gives you idea how to simply make value limited to 4 decimal points. If you want to lower or upper the numbers after decimal, just change 4 to whatever you want.


SELECT CAST(Value as Decimal(10,2)) FROM TABLE_NAME;

Would give you 2 values after the decimal point. (MS SQL SERVER)


Mod(x,1) is the easiest way I think.


select convert(int,@value)

참고URL : https://stackoverflow.com/questions/44046/truncate-not-round-decimal-places-in-sql-server

반응형