Skip to main content

Numeric Functions

SQL numeric functions are operations used to perform mathematical computations on numeric data types in a database. They include functions like ABS (returns the absolute value), ROUND (rounds a number to a specified number of decimal places), CEILING (returns the smallest integer greater than or equal to a given number), FLOOR (returns the largest integer less than or equal to a given number), and SUM (calculates the sum of values). These functions help manipulate numeric data within SQL queries for various analytical and reporting purposes.


ABS: The SQL ABS function returns the absolute value of a numeric expression. This means it returns the positive value of the given number, regardless of its sign. Here's an example to illustrate its usage.

SELECT Number, ABS(Number) AS AbsoluteValue FROM Numbers;



In this example, the ABS function is applied to each value in the Number column of the Numbers table, returning its absolute value. So, negative numbers become positive, and positive numbers remain unchanged.

 

ROUND: The SQL ROUND function is used to round a numeric value to a specified number of decimal places.

Consider a table named ProductSales with the following columns: ProductID and UnitPrice. We want to retrieve the rounded unit prices of products to two decimal places.

SELECT ProductID, ROUND(UnitPrice, 2) AS RoundedPrice FROM ProductSales;

In this example, the ROUND function is applied to the UnitPrice column, rounding each value to two decimal places. The result set will contain the ProductID along with the rounded prices in the RoundedPrice column.

 

CEILING: The SQL CEILING function returns the smallest integer greater than or equal to a specified numeric expression.

Consider a table named Orders with the following columns: OrderID and TotalAmount. We want to retrieve the ceiling value of the total amount for each order.

SELECT OrderID, CEILING(TotalAmount) AS RoundedTotalAmount FROM Orders;

In this example, the CEILING function is applied to the TotalAmount column, returning the smallest integer greater than or equal to each total amount. The result set will include the OrderID along with the rounded total amount in the RoundedTotalAmount column.

 

FLOOR: The SQL FLOOR function returns the largest integer less than or equal to a specified numeric expression.

Consider a table named Products with the following columns: ProductID and Price. We want to retrieve the floor value of the price for each product.

SELECT ProductID, FLOOR(Price) AS FlooredPrice FROM Products;

In this example, the FLOOR function is applied to the Price column, returning the largest integer less than or equal to each price. The result set will include the ProductID along with the floored price in the FlooredPrice column.

 

SQRT: The SQL SQRT function is used to calculate the square root of a numeric expression.

Consider a table named Circle with the following columns: CircleID and Radius. We want to calculate the square root of the radius for each circle.

SELECT CircleID, SQRT(Radius) AS SquareRootRadius FROM Circle;

In this example, the SQRT function is applied to the Radius column, computing the square root of each radius. The result set will include the CircleID along with the calculated square root of the radius in the SquareRootRadius column.

 


Comments

Popular posts from this blog

Performance Optimization

Performance optimization in SQL is crucial for ensuring that your database queries run efficiently, especially as the size and complexity of your data grow. Here are several strategies and techniques to optimize SQL performance: Indexing Create Indexes : Primary Key and Unique Indexes : These are automatically indexed. Ensure that your tables have primary keys and unique constraints where applicable. Foreign Keys : Index foreign key columns to speed up join operations. Composite Indexes : Use these when queries filter on multiple columns. The order of columns in the index should match the order in the query conditions. Avoid Over-Indexing:  Too many indexes can slow down write operations (INSERT, UPDATE, DELETE). Only index columns that are frequently used in WHERE clauses, JOIN conditions, and as sorting keys. Query Optimization Use SELECT Statements Efficiently : SELECT Only Necessary Columns : Avoid using SELECT * ; specify only ...

DAX UPPER Function

The DAX UPPER function in Power BI is used to convert all characters in a text string to uppercase. This function is useful for standardizing text data, ensuring consistency in text values, and performing case-insensitive comparisons. Syntax: UPPER(<text>) <text>: The text string that you want to convert to uppercase. Purpose: The UPPER function helps ensure that text data is consistently formatted in uppercase. This can be essential for tasks like data cleaning, preparing text for comparisons, and ensuring uniformity in text-based fields. E xample: Suppose you have a table named "Customers" with a column "Name" that contains names in mixed case. You want to create a new column that shows all names in uppercase. UppercaseName = UPPER(Customers[Name]) Example Scenario: Assume you have the following "Customers" table: You can use the UPPER function as follows: Using the UPPER function, you can convert all names to uppercase: UppercaseName = ...

Understanding the Power BI ecosystem and workflow

Understanding the Power BI ecosystem and workflow involves getting familiar with the various components of Power BI and how they interact to provide a comprehensive data analysis and visualization solution. Here's a detailed explanation: Power BI Ecosystem The Power BI ecosystem consists of several interconnected components that work together to enable users to connect to data sources, transform and model data, create visualizations, and share insights. The main components are: Power BI Desktop Power BI Service Power BI Mobile Power BI Gateway Power BI Report Server Power BI Embedded PowerBI Workflow Here’s a typical workflow in the Power BI ecosystem: Step 1: Connect to Data Sources Power BI Desktop:  Connect to various data sources like Excel, SQL databases, cloud services, and more. Power BI Gateway:  If using on-premises data sources, install and configure the gateway for secure data transfer. Step 2: Data Transformation and Modeling Power BI Desktop:  Use Power Query...