Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : 2 Ways to Execute Operating System Commands From SQL Server Management Studio
SQL Server Helper
Home > > Tip of the Day
SQL Server Helper - Tip of the Day

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