Tip of the Day : Generate Random Password User-Defined Function

SQL Server Helper - Tip of the Day

How to Call GETDATE Function in a User-Defined Function

Since the GETDATE() function is a non-deterministic function, this function cannot be called from another function.  One way to work around this limitation is to call the function indirectly using a view.  First create a view that calls the GETDATE() function:

CREATE VIEW [dbo].[CurrentDate] AS 
SELECT GETDATE() AS [CurrentDate]

Since a view can be accessed from a function, a user-defined function can now be created that will indirectly call the GETDATE() function through the view:

CREATE FUNCTION [dbo].[ufn_GetDate]() RETURNS DATETIME
AS
BEGIN
    RETURN (SELECT [CurrentDate] FROM [dbo].[CurrentDate])
END

Now to use this user-defined function

SELECT [dbo].[ufn_GetDate]()

Back to Tip of the Day List Next Tip