How does a SQL Server function work?

A function accepts input parameters, performs a specific action—which may range from simple calculations to complex manipulations with data—and then returns the result of that action as either a single value or a result set. You can write a function once and reuse it whenever it's required. Another thing worth mentioning is that SQL Server functions cannot be used to insert, update, or delete records in database tables.

String Functions

Function Description
ASCII Returns the corresponding ASCII value for the specified character
CHAR Returns a character based on the ASCII code
CHARINDEX Returns the position of a substring in a string
CONCAT / CONCAT with + Adds two or more strings together
CONCAT_WS Adds two or more strings together using a separator
DATALENGTH Returns the number of bytes used to represent a specified expression
DIFFERENCE Compares two SOUNDEX values and returns an integer value
FORMAT Formats a value with the indicated format
LEFT Returns a specified number of characters from a string (starting from left)
LEN Returns the length of a string
LOWER Converts letters in a string to lowercase
LTRIM Deletes all leading spaces from a string
NCHAR Returns the Unicode character based on the number code
PATINDEX Returns the position of a pattern in a string
QUOTENAME Adds delimiters to a Unicode string and returns it as a valid delimited identifier
REPLACE Searches for a specified substring in a string and replaces it with another substring
REPLICATE Repeats a string an indicated number of times
REVERSE Returns a reversed string
RIGHT Returns a specified number of characters from a string (starting from right)
RTRIM Deletes all trailing spaces from a string
SOUNDEX Returns a four-character code to assess the similarity of two specified strings
SPACE Returns a string with the specified number of spaces
STR Converts a value into a string
STUFF Deletes a specified number of characters from a specified position in a string and then inserts another string, starting at the said position
SUBSTRING Returns characters from a specified position in a string
TRANSLATE Returns the string from the first argument after the characters indicated in the second argument are translated into the characters indicated in the third argument
TRIM Deletes all leading and trailing spaces—or other characters—from a string
UNICODE Returns the Unicode value for the first character of a specified expression
UPPER Converts letters in a string to uppercase

Mathematical Functions

Function Description
ABS Returns the absolute value of a number
ACOS Returns the arc cosine of a number
ASIN Returns the arc sine of a number
ATAN Returns the arc tangent of a number
ATN2 Returns the arc tangent of two numbers
AVG Returns the average value of a specified expression
CEILING Returns the smallest integer value that is larger than or equal to a number
COUNT Returns the number of records returned by a SELECT query
COS Returns the cosine of a number
COT Returns the cotangent of a number
DEGREES Converts a value in radians to degrees
EXP Returns the e constant raised to the power of a specified number
FLOOR Returns the largest integer value that is smaller than or equal to a number
LOG Returns the natural logarithm of a number—or the logarithm of a number to a specified base
LOG10 Returns the natural logarithm of a number to base 10
MAX Returns the maximum value in a specified set of values
MIN Returns the minimum value in a specified set of values
PI Returns the value of the pi constant
POWER Returns the value of a number raised to the power of another number
RADIANS Converts a value in degrees to radians
RAND Returns a random number
ROUND Rounds a number to a specified number of decimal places
SIGN Returns the sign of a number
SIN Returns the sine of a number
SQRT Returns the square root of a number
SQUARE Returns the square of a number
SUM Calculates the sum of a specified set of values
TAN Returns the tangent of a number

Date Functions

Function Description
CURRENT_TIMESTAMP Returns the current date and time
DATEADD Adds a time/date interval to a date and then returns the date
DATEDIFF Returns the difference between two dates
DATEFROMPARTS Compiles a date from the specified year, month, and day values
DATENAME Returns a specified part of a date as a string
DATEPART Returns a specified part of a date as an integer value
DAY Returns the day of the month from a specified date
GETDATE Returns the current database systemdate and time
GETUTCDATE Returns the current database system UTC date and time
ISDATE Checks an expression that contains a date; returns 1 if the date is valid, otherwise returns 0
MONTH Returns the month from a specified date as a number from 1 to 12
SYSDATETIME Returns the date and time of the machine that SQL Server is running on
YEAR Returns the year from a specified date

Advanced Functions

Function Description
CAST Converts a value into a specified datatype
COALESCE Returns the first non-null value from a list
CONVERT Converts a value into a specified datatype
CURRENT_USER Returns the name of the current user in a SQL Server database
IIF Returns a value if a condition is TRUE or another value if a condition is FALSE
ISNULL Returns a specified value if the expression is NULL, otherwise returns the expression
ISNUMERIC Checks whether an expression is numeric
NULLIF Returns NULL if two specified expressions are equal
SESSION_USER Returns the name of the current user in a SQL Server database
SESSIONPROPERTY Returns the session settings for a specified option
SYSTEM_USER Returns the login name for the current user
USER_NAME Returns the database user name based on the specified ID

Try it yourself with dbForge tools for SQL Server

dbForge SQL Complete
dbForge SQL Complete is a high-end add-in for SSMS and Visual Studio that delivers advanced SQL code completion, formatting, refactoring, and debugging.
dbForge Studio for SQL Server
dbForge Studio is an all-encompassing IDE that covers nearly all aspects of SQL Server development, management, administration, data analysis and reporting.