Mosh Feu Mosh Feu - 6 months ago 39
SQL Question

How to calculate decimal(x, y) max value in SQL Server

How do I know the maximum value of

decimal
type?

For example:

decimal(5, 2)


Can you please explain the mechanism of
decimal
type?

Answer

You can use an aggregate function in order to get the maximum value in a column

SELECT MAX(myColumn) AS MyColumnCount
FROM myTable

If you want to know the maximum value for a customer for instance, you can group by this customer

SELECT CustomerID, MAX(myColumn) AS MyColumnCount
FROM myTable
GROUP BY CustomerID

You can find other aggregate functions here.


If, by contrast, you are interested in range of a decimal type, then consider, that you are declaring the total number of digits and decimals. Therefore the maximum number is reached, when all these digits are 9.

So for decimal(5,2) it would be 999.99. 5 is the total number of decimals to the left and to the right of the decimal point. 2 is the number of decimals to the right of the decimal point.

The maximum possible range for decimals is -10^38 + 1 through 10^38 - 1.