Home >Database >Mysql Tutorial >How Can I Truncate Decimal Places in SQL Server Without Rounding?

How Can I Truncate Decimal Places in SQL Server Without Rounding?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-15 09:51:44805browse

How Can I Truncate Decimal Places in SQL Server Without Rounding?

SQL Server Decimal Truncation: A Guide

Working with decimals in SQL Server often requires precise control over decimal places. Sometimes, you need to remove extra digits without the effect of rounding. While SQL Server's ROUND() function is primarily for rounding, it can also truncate.

To truncate, use the ROUND() function with a non-zero third argument. A zero in this position signifies rounding; any other value performs truncation.

Let's illustrate:

<code class="language-sql">DECLARE @value DECIMAL(18, 2);
SET @value = 123.456;</code>

Normally, @value would round to 123.46. To truncate:

<code class="language-sql">SELECT ROUND(@value, 2, 1);</code>

This returns 123.45, effectively truncating the extra decimal place.

Function Syntax and Parameters

The ROUND() function follows this structure:

<code class="language-sql">ROUND(numeric_expression, length [ ,function ])</code>

Here's a breakdown of the arguments:

Argument Description
numeric_expression The decimal value you want to truncate.
length The desired precision (number of decimal places). Positive values specify decimal places to the right of the decimal point; negative values specify places to the left.
function The operation type. 0 rounds; any non-zero value truncates.

Important Considerations

  • The length parameter can be positive or negative. Positive values control decimal places to the right of the decimal point, while negative values affect digits to the left.
  • The function parameter is optional; it defaults to 0 (rounding) if omitted.

This method provides a straightforward way to truncate decimal values in SQL Server without resorting to more complex workarounds.

The above is the detailed content of How Can I Truncate Decimal Places in SQL Server Without Rounding?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn