Functions

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008)noAzure SQL DatabasenoAzure SQL Data Warehouse yesParallel Data Warehouse

SQL Server provides many built-in functions and also lets you create user-defined functions. The categories of built-in functions are listed on this page.

Aggregate functions

Aggregate functions perform a calculation on a set of values and return a single value. They are allowed in the select list or the HAVING clause of a SELECT statement. You can use an aggregation in combination with the GROUP BY clause to calculate the aggregation on categories of rows. Use the OVER clause to calculate the aggregation on a specific range of value. The OVER clause cannot follow the GROUPING or GROUPING_ID aggregations.

All aggregate functions are deterministic, which means they always return the same value when they run on the same input values. For more information, see Deterministic and Nondeterministic Functions.|

Analytic functions

Analytic functions compute an aggregate value based on a group of rows. However, unlike aggregate functions, analytic functions can return multiple rows for each group. You can use analytic functions to compute moving averages, running totals, percentages or top-N results within a group.

Ranking functions

Ranking functions return a ranking value for each row in a partition. Depending on the function that is used, some rows might receive the same value as other rows. Ranking functions are nondeterministic.

Rowset functions

Rowset functions Return an object that can be used like table references in an SQL statement.

Scalar functions

Operate on a single value and then return a single value. Scalar functions can be used wherever an expression is valid.

Categories of scalar functions

Function category Description
Configuration Functions Return information about the current configuration.
Conversion Functions Support data type casting and converting.
Cursor Functions Return information about cursors.
Date and Time Data Types and Functions Perform operations on a date and time input values and return string, numeric, or date and time values.
JSON Functions Validate, query, or change JSON data.
Logical Functions Perform logical operations.
Mathematical Functions Perform calculations based on input values provided as parameters to the functions, and return numeric values.
Metadata Functions Return information about the database and database objects.
Security Functions Return information about users and roles.
String Functions Perform operations on a string (char or varchar) input value and return a string or numeric value.
System Functions Perform operations and return information about values, objects, and settings in an instance of SQL Server.
System Statistical Functions Return statistical information about the system.
Text and Image Functions Perform operations on text or image input values or columns, and return information about the value.

Function Determinism

SQL Server built-in functions are either deterministic or nondeterministic. Functions are deterministic when they always return the same result any time they are called by using a specific set of input values. Functions are nondeterministic when they could return different results every time they are called, even with the same specific set of input values. For more information, see Deterministic and Nondeterministic Functions

Function Collation

Functions that take a character string input and return a character string output use the collation of the input string for the output.

Functions that take non-character inputs and return a character string use the default collation of the current database for the output.

Functions that take multiple character string inputs and return a character string use the rules of collation precedence to set the collation of the output string. For more information, see Collation Precedence (Transact-SQL).

See Also

CREATE FUNCTION (Transact-SQL)
Deterministic and Nondeterministic Functions
Using Stored Procedures (MDX)