Microsoft SQL Server
NUMERIC functions
In Microsoft SQL Server, numeric functions are used to perform calculations and manipulations on numeric data types such as integers, decimals, and floats. These functions allow you to perform tasks like rounding numbers, calculating absolute values, finding square roots, and more. For beginners, learning to use these numeric functions is essential for performing calculations on sales data, product prices, customer balances, and more.
Below is a detailed explanation of commonly used numeric functions with examples and best practices.
1. ABS() – Absolute Value
The ABS() function returns the absolute (positive) value of a number.
SELECT ABS(column_name) AS AbsoluteValue FROM table_name;
Example:
SELECT ProductName, ABS(Profit) AS PositiveProfit FROM Products;
This query returns the absolute value of the profit for each product, ensuring no negative values.
2. CEILING() – Ceiling Value
The CEILING() function returns the smallest integer greater than or equal to the given number.
SELECT CEILING(column_name) AS CeilingValue FROM table_name;
Example:
SELECT ProductName, CEILING(Price) AS RoundedPriceUp FROM Products;
This query rounds the price of each product up to the nearest whole number.
3. FLOOR() – Floor Value
The FLOOR() function returns the largest integer less than or equal to the given number.
SELECT FLOOR(column_name) AS FloorValue FROM table_name;
Example:
SELECT ProductName, FLOOR(Price) AS RoundedPriceDown FROM Products;
This query rounds the price of each product down to the nearest whole number.
4. ROUND() – Rounding Numbers
The ROUND() function rounds a number to a specified number of decimal places.
SELECT ROUND(column_name, decimal_places) AS RoundedValue FROM table_name;
Example:
SELECT ProductName, ROUND(Price, 2) AS RoundedPrice FROM Products;
This query rounds the price of each product to two decimal places.
5. POWER() – Exponentiation
The POWER() function raises a number to the power of another number.
SELECT POWER(base, exponent) AS Result FROM table_name;
Example:
SELECT POWER(2, 3) AS PowerResult;
This query returns 2 raised to the power of 3, which equals 8.
6. SQRT() – Square Root
The SQRT() function returns the square root of a given number.
SELECT SQRT(column_name) AS SquareRoot FROM table_name;
Example:
SELECT SQRT(Price) AS SquareRootOfPrice FROM Products;
This query returns the square root of each product's price.
7. LOG() – Natural Logarithm
The LOG() function returns the natural logarithm (base e) of a number.
SELECT LOG(column_name) AS NaturalLog FROM table_name;
Example:
SELECT LOG(Price) AS LogOfPrice FROM Products;
This query returns the natural logarithm of each product's price.
8. EXP() – Exponential Value
The EXP() function returns the exponential value of a number (base e raised to the power of the number).
SELECT EXP(column_name) AS ExponentialValue FROM table_name;
Example:
SELECT EXP(1) AS ExponentialValue;
This query returns the value of e raised to the power of 1.
9. Best Practices for Using Numeric Functions
Use
ABS()to Handle Negative Values – TheABS()function is useful when you want to ensure that numeric data, such as financial losses or debts, is always displayed as a positive value.SELECT CustomerName, ABS(Balance) AS PositiveBalance FROM Customers;Round Numbers with
ROUND()for Precision – UseROUND()when working with financial or calculated data to ensure that numbers are displayed to a precise number of decimal places.SELECT ROUND(SaleAmount, 2) AS RoundedSaleAmount FROM Sales;Choose
CEILING()orFLOOR()for Whole Number Calculations – UseCEILING()to round numbers up andFLOOR()to round them down when working with prices, inventory counts, or other data that needs to be represented as whole numbers.SELECT CEILING(TotalPrice) AS RoundedUpPrice FROM Orders;Use
POWER()andSQRT()for Mathematical Operations – These functions are useful for scientific or statistical calculations. For example, calculating the square root of a product’s price for advanced analysis.SELECT ProductName, SQRT(Price) AS PriceSquareRoot FROM Products;Ensure Compatibility of Data Types – When using numeric functions, make sure that the data types of the columns you are working with are compatible with the function. For example, ensure that you are not passing text data into a numeric function.
Handle Edge Cases – Be mindful of special cases when working with functions like
SQRT()orLOG()that don’t work with negative numbers or zero.SELECT SQRT(CASE WHEN Value >= 0 THEN Value ELSE NULL END) AS SafeSquareRoot FROM Data;
By mastering numeric functions in SQL Server, you can perform complex calculations and manipulations directly in your queries, making it easier to analyze data such as prices, sales figures, and product information. These functions are vital for handling numeric data effectively in SQL Server.
To gain complete access, login with gmail or outlook, no need of signup, click here


Comments Not Found