The Wayback Machine - https://web.archive.org/web/20240721091901/https://www.geeksforgeeks.org/deterministic-and-nondeterministic-functions-in-sql-server/
Open In App

Deterministic and Nondeterministic Functions in SQL Server

Last Updated : 10 Jun, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Deterministic and nondeterministic functions are two fundamental concepts in SQL Server that determine how a function behaves when called with a specific set of input values.

Deterministic functions always return the same result every time they are called with a fixed set of input values and given the same state of the database. On the other hand, nondeterministic functions may return different results each time they are called with a fixed set of input values, even if the database state remains the same.

Understanding the determinism of functions is crucial for ensuring the reliability and performance of SQL Server queries. In this article, we will understand deterministic and non-deterministic functions in SQL Server.

Deterministic functions

Deterministic functions always result in the same output every time they are called with a fixed set of input values and given the same condition of the database.

For example, the AVG() function always results in the same result, given the qualifications stated above.

Deterministic built-in functions

Here is the list of deterministic built-in functions in SQL Server.

ABS DATEDIFF
POWER ACOS
DAY RADIANS
ASIN DEGREES
ROUND ATAN
EXP SIGN
ATN2 FLOOR
FLOOR SIN
CEILING ISNULL
SQUARE COALESCE
ISNUMERIC SQRT
DATEADD NULLIF
COS LOG10
LOG YEAR
TAN DATALENGTH
COT MONTH

The below functions are not always deterministic but could be deterministic when they are specified in a deterministic manner:

  • CAST is deterministic until used with DateTime, smalldatetime, or sql_variant.
  • ISDATE is deterministic only in case used with CONVERT function.
  • CONVERT is deterministic until one of these conditions exists.
    1. Source type may be sql_variant.
    2. Target type may sql_variant & its source type is nondeterministic.

Nondeterministic functions

Nondeterministic functions result in different output each time they are called with a fixed set of input values even if the database state that they access remains the same.

For example, GETDATE() function, results the current date and time value, always a different value.

Built-in Function Determinism

You cannot impact the determinism of any built-in function. The built-in function might be deterministic or nondeterministic based on the property of the function, implemented by SQL Server. For example, using an ORDER BY clause in any query do not change the determinism of the function that is used in the query.

Nondeterministic built-in functions

Here is the list of nondeterministic built-in functions in SQL Server:

@@CONNECTIONS LAG
@@DBTS LAST_VALUE
@@IDLE LEAD
@@CPU_BUSY MIN_ACTIVE_ROWVERSION
@@IO_BUSY NEWID
@@PACK_RECEIVED NEWSEQUENTIALID
@@MAX_CONNECTIONS NEXT VALUE FOR
@@PACK_SENT NTILE
@@PACKET_ERRORS PARSENAME
@@TIMETICKS PERCENTILE_CONT
@@TOTAL_ERRORS PERCENTILE_DISC
@@TOTAL_READ CUME_DIST
@@TOTAL_WRITE PERCENT_RANK
GETUTCDATE FORMAT
GETDATE RAND
GET_TRANSMISSION_STATUS DENSE_RANK
CURRENT_TIMESTAMP FIRST_VALUE
RANK ROW_NUMBER

Conclusion

Deterministic and nondeterministic functions are essential concepts in SQL Server that determine how a function behaves when called with a specific set of input values. Understanding the determinism of functions is crucial for ensuring the reliability and performance of SQL Server queries



Similar Reads

Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Dynamic SQL and Temporary Tables in SQL Server
In SQL Server, creating and using Temp Tables using dynamic SQL is a good feature when we need to temporarily create tables at run time and delete automatically all within a session. They can be very useful when we need to store temporary data in a structured format and do data manipulation using Data Manipulation Language in SQL. In this article l
6 min read
SQL Server Group Functions
The group function in SQL Server provides a powerful tool for performing calculations on groups of rows, allowing you to group data based on specific criteria. This function is important when you want to analyze and summarize information from multiple records in a data structure. The basic group functions are COUNT, SUM, AVG, MAX, and MIN. Syntax:
3 min read
SQL Server Window Functions ROWS vs. RANGE
We will cover the important concept of the MS SQL Server which is the difference between Row and Range. The confusion between the row and range and where it is used will be eradicated by reading this article. So, be with the flow of the article for a better understanding and keep practicing side by side. Understanding this topic will help you in be
5 min read
SQL Server Mathematical Functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
SQL Server mathematical functions are used to perform mathematical calculations. These functions are designed to simplify complex calculations and make it easier to analyze and manipulate data. Here, we will cover the syntax and examples of various mathematical functions like SQRT(), PI(), SQUARE(), and many more Mathematical Functions in SQL Serve
1 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
Article Tags :
three90RightbarBannerImg