FUNCTIONS IN MSSQL

,

In Microsoft SQL Server, functions are database objects that perform a specific task or computation.

Functions are stored permanantly in database and used for easy end user access and reports.

SQL Server provides various types of functions, including scalar functions, table-valued functions, and aggregate functions. Here are the main types of functions in SQL Server:

1. Scalar Value Returning Functions:

Scalar functions returns single value as the output.

  1. Built-in Scalar Functions:
  • SQL Server provides a variety of built-in scalar functions that operate on a single value and return a single value.

Here’s a table presenting some commonly used built-in scalar functions in Microsoft SQL Server:

Function categoryDescription
Configuration FunctionsReturn information about the current configuration.
Conversion FunctionsSupport data type casting and converting.
Cursor FunctionsReturn information about cursors.
Date and Time Data Types and FunctionsPerform operations on a date and time input values and return string, numeric, or date and time values.
Graph FunctionsPerform operations to convert to and from character representations of graph node and edge IDs.
JSON FunctionsValidate, query, or change JSON data.
Logical FunctionsPerform logical operations.
Mathematical FunctionsPerform calculations based on input values provided as parameters to the functions, and return numeric values.
Metadata FunctionsReturn information about the database and database objects.
Security FunctionsReturn information about users and roles.
String FunctionsPerform operations on a string (char or varchar) input value and return a string or numeric value.
System FunctionsPerform operations and return information about values, objects, and settings in an instance of SQL Server.
System Statistical FunctionsReturn statistical information about the system.
Text and Image FunctionsPerform operations on text or image input values or columns, and return information about the value.
Table source from Microsoft website

For a more detailed reference and to explore additional functions, refer to the official Microsoft documentation: Scalar Functions (Transact-SQL).

  1. User-Defined Scalar Functions:
  • Users can create their own scalar functions to encapsulate custom logic. These functions can be used in SELECT, WHERE, and other clauses.

Syntax:

Example:

A simple example is to calculate sum of three numbers.

The above function has to be called with parameter as follows.

Output:

2. Table-Valued Functions:

  1. Inline Table-Valued Functions (iTVF):
  • These functions return a table-like result set and are typically used in the FROM clause of a SELECT statement.
  • These functions store single statement in the function definition.

Syntax:

Example:

Let us create some sample tables for demonstration.

In order to find the Product sales details of a particular product category, we will write the following select query.

We can now pack the above logic into a function, so we can reuse the function for different product categories.

Output:

In order to execute the function, we need to call them as below.

The above function provides a table containing the Sales details of Beauty Products.

Similarly, we can call the same function for other products.

  1. Multi-Statement Table-Valued Functions (mTVF):
  • These functions use a BEGIN…END block to define the function logic and return a result set in a table format.
  • These functions store multiple statements in the function definition.
  • Example : To report a list of alphanumeric sequence values, To generate calendar data etc.

Syntax:

Example 1:

Generate a list of sequence numbers.

To execute the above function, we need to execute the following statement.

The above function generates the sequence number from 1 to 100.

Output:

Example 2:

From the PRODUCTS table we created before, we can create a function to get highly priced products.

To call the function, we execute the following.

3. Aggregate functions:

  • Operate on sets of values to return a single value.
  • Examples include SUM, AVG, COUNT, MIN, MAX, etc.

To learn more about aggregate functions, please follow this link. aggregate-functions-in-mssql

4. Windows functions:

  • Perform calculations across a specified range of rows.
  • Examples include ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG, etc.

To learn more about Windows aggregate functions, please follow this link. windows-functions-in-mssql

5. Rowset functions:

OPENROWSET and OPENQUERY can be used to access data from external data sources.

To learn more about Rowset functions, please follow this link. rowset-functions-in-mssql

Summary:

  • Functions are reusable code or objects with a predefined operation to perform a specific task.
  • Functions can be system built in or user defined.
  • Microsoft provides a variety of functions for a wide range of data operations.
  • The functions are grouped into 5 categories in this blog. These are ,
    • Scalar value returning functions
    • Table valued functions
    • Aggregate functions
    • Windows functions
    • Rowset functions

References:

  1. https://learn.microsoft.com/en-us/sql/t-sql/functions/functions?view=sql-server-ver16#scalar-functions
  2. https://www.sqlite.org/lang_corefunc.html
  3. date-functions-in-mssql
  4. string-functions-in-mssql

Popular posts