Functions
The Transact-SQL programming language provides three types of functions:
- Rowset functions
Can be used like table references in an SQL statement. For more information about a list of these functions, see Rowset Functions.
- Aggregate functions
Operate on a collection of values but return a single, summarizing value. For more information about a list of these functions, see Aggregate Functions.
- Scalar functions
Operate on a single value and then return a single value. Scalar functions can be used wherever an expression is valid. This table categorizes the scalar functions.
Function category Explanation Configuration Functions Returns information about the current configuration. Cursor Functions Returns information about cursors. Date and Time Functions Performs an operation on a date and time input value and returns either a string, numeric, or date and time value. Mathematical Functions Performs a calculation based on input values provided as parameters to the function, and returns a numeric value. Metadata Functions Returns information about the database and database objects. Security Functions Returns information about users and roles. String Functions Performs an operation on a string (char or varchar) input value and returns a string or numeric value. System Functions Performs operations and returns information about values, objects, and settings in Microsoft® SQL Server™. System Statistical Functions Returns statistical information about the system. Text and Image Functions Performs an operation on a text or image input values or column, and returns information about the value.
Function Determinism
SQL Server 2000 built-in functions are either deterministic or nondeterministic. Functions are deterministic when they always return the same result any time they are called with a specific set of input values. Functions are nondeterministic when they could return different results each time they are called, even with the same specific set of input values.
The determinism of functions dictate whether they can be used in indexed computed columns and indexed views. Index scans must always produce consistent results. Thus, only deterministic functions can be used to define computed columns and views that are to be indexed.
Configuration, cursor, meta data, security, and system statistical functions are nondeterministic. In addition, the following built-in functions are also always nondeterministic:
@@ERROR | FORMATMESSAGE | NEWID |
@@IDENTITY | GETANSINULL | PERMISSIONS |
@@ROWCOUNT | GETDATE | SESSION_USER |
@@TRANCOUNT | HOST_ID | STATS_DATE |
APP_NAME | HOST_NAME | SYSTEM_USER |
CURRENT_TIMESTAMP | IDENT_INCR | TEXTPTR |
CURRENT_USER | IDENT_SEED | TEXTVALID |
DATENAME | IDENTITY | USER_NAME |
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.