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

Power BI tenant settings and admin portal

As of my last update, Power BI offers a dedicated admin portal for managing settings and configurations at the tenant level. Here's an overview of Power BI tenant settings and the admin portal: 1. Power BI Admin Portal: Access : The Power BI admin portal is accessible to users with admin privileges in the Power BI service. URL : You can access the admin portal at https://app.powerbi.com/admin-portal . 2. Tenant Settings: General Settings : Configure general settings such as tenant name, regional settings, and language settings. Tenant Administration : Manage user licenses, permissions, and access rights for Power BI within the organization. Usage Metrics : View usage metrics and reports to understand how Power BI is being used across the organization. Service Health : Monitor the health status of the Power BI service and receive notifications about service incidents and outages. Audit Logs : Access audit logs to track user activities, access requests, and administrative actions wit...

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 ...

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...