Home > Database > Mysql Tutorial > How to Format Numbers with Two Decimal Places in SQL Server?

How to Format Numbers with Two Decimal Places in SQL Server?

Patricia Arquette
Release: 2025-01-24 21:42:40
Original
864 people have browsed it

How to Format Numbers with Two Decimal Places in SQL Server?

Formatting Numbers to Two Decimal Places in SQL Server

This guide explains how to display numeric values with exactly two decimal places in SQL Server. This is a common requirement when presenting financial or other precise data.

The CONVERT function provides the solution. Its syntax is:

<code class="language-sql">CONVERT(DECIMAL(precision, scale), numeric_expression)</code>
Copy after login

Where:

  • precision: The total number of digits (both before and after the decimal point).
  • scale: The number of digits after the decimal point. Set this to 2 for two decimal places.
  • numeric_expression: The number you want to format.

For instance, to format 2.999999 to show two decimal places:

<code class="language-sql">SELECT CONVERT(DECIMAL(10, 2), 2.999999);</code>
Copy after login

This will return 3.00. Note that SQL Server rounds the number.

Important: The DECIMAL data type is crucial for this task. Unlike FLOAT or REAL, DECIMAL offers fixed precision and scale, ensuring accurate representation of decimal values. Choosing the appropriate precision value depends on the expected range of your numbers.

The above is the detailed content of How to Format Numbers with Two Decimal Places in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template