GETDATE() in a UDF

In SQL Server 2000 Built-in nondeterministic functions were not allowed in the body of a user-defined functions (UDF).  If you attempted to create a UDF that used GETDATE() you received an error similar to:

Server: Msg 443, Level 16, State 1, Procedure ufn_Getdate, Line 5
Invalid use of ‘getdate’ within a function.

In SQL Server 2005 nondeterministic Built-in functions can be used in the body of a UDF with the exception of the following Built-in functions NEWID, RAND, NEWSEQUENTIALID, and TEXTPTR.

This means that you can now use GETDATE() inside a UDF.  For example:

CREATE FUNCTION dbo.ufn_Getdate()
RETURNS DATETIME
AS

BEGIN
RETURN GETDATE()
END

GO

SELECT dbo.ufn_Getdate() AS TheDate

Returns:

TheDate
———————–
2006-02-15 14:44:57.800

(1 row(s) affected)