SQL Functions
SQL Functions
Function in SQL Server are database objects that include a group of SQL statements to
perform a specified activity. A function takes parameters, performs actions, and returns
the outcome. It should be noted that functions always return either a single value or a
table.
A function must be given a name, which cannot begin with a special character such as
@, $, #, or other similar characters.
The only statements that work with functions are SELECT statements.
AVG, COUNT, SUM, MIN, DATE, and other functions can be used anywhere with the
SELECT query in SQL.
Types of Function
System Functions
User-Defined Functions
These functions are defined by SQL Server for different purposes. The functions that
are defined by the system are known as "system-defined functions". In other words, all
the built-in functions supported by the SQL server are referred to as system-defined
functions. Usage of the built-in functions saves much development time while
performing certain tasks. These types of functions generally work with the SQL select
statement to calculate the values and the manipulated data. We have two types of
system-defined functions in the SQL Server
1. Scalar Function
Scalar functions operate on a single value and return a single value. Below is a list of
some useful Scalar Functions in SQL Server.
2. Aggregate Function
Aggregate functions operate on a collection of values and return a single value. Below is
a list of some useful Aggregate Functions in SQL Server.
2. User-Defined Function
Scalar Function
The user-defined scalar function also returns a single value due to actions performed by
the function. We return any datatype value from a function.
Inline Table Valued Functions: These functions return a Table Set with just one
TSQL statement.
Multiple Statements Table Valued Functions: (Returns Table Set; contains many
TSQL statements).
When a value from the first table matches every value from the second table, SQL ANY
returns the row with the matching value.
Aggregate functions and scalar functions are the two categories under which SQL
functions fall.
You can list functions in the current database or in a different database by using the
SHOW FUNCTIONS command. The names of each function and the identity of the
function definer are returned by the function list.