# Friday, March 11, 2005
« Tip of the Day -- Great Exception Handli... | Main | Tip of the day: Renaming files with a ti... »

Someone asked me today if I knew that you couldn't use GETDATE() in functions in SQL Server. Well, I did, so I dug out this solution I had found a while back, and decided to post it, since I am sure others have come across the same issue.

Basically, you use a view to do the work for you... here you go!

CREATE VIEW Function_Assist_GETDATE

/********************************************************
*
* A view to return one row, with one column, the current
* date/time from the built-in function GETDATE().  This
* view allows a UDF to bypass the restriction on access to
* the non-deterministic getdate() function.
*
* Attribution: Based on a newsgroup posting in by Mikhail
*   Berlyant in microsoft.public.sqlserver.programming
*
* Common Usage:
DECLARE @dtVar datetime
select @dtVAr = [GetDate] from Function_Assist_GETDATE
**********************************************************/

AS
    SELECT getdate() as [GetDate]

GO

GRANT SELECT on Function_Assist_GETDATE to PUBLIC
GO

Comments are closed.