dbTalk Databases Forums  

GetDate() function

comp.database.ms-sqlserver comp.database.ms-sqlserver


Discuss GetDate() function in the comp.database.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
axism (Offline)
Junior Member
 
Posts: 3
Join Date: May 2006

Question GetDate() function - 05-12-2006 , 10:19 AM






I m trying to write a function that calculates date difference as follows:

ALTER FUNCTION dbo.daycount(@startday DateTime)
RETURNS Integer AS
BEGIN

DECLARE @enddate DateTime
Select @enddate = GetDate()

RETURN DATEDIFF(day, @startday, @enddate)

END


keeps saying I can't use GetDate(). Can someone tell me what's wrong with this?

PS, I just did some search on web. Everyone is saying you cant using it in SQL2k.

Question is, there any way around it?

Last edited by axism : 05-12-2006 at 03:22 PM .

Reply With Quote
  #2  
Old   
Jack Vamvas
 
Posts: n/a

Default Re: GetDate() function - 05-15-2006 , 11:13 AM






Built in function s that are non deterministic can't be nested in another
UDF .The getdate() function is non deterministic as it returns a different
result every time with the same parameters/


--
----
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
___________________________________


"axism" <axism.27pc60 (AT) no-mx (DOT) forums.yourdomain.com.au> wrote

Quote:
I m trying to write a function that calculates date difference as
follows:

ALTER FUNCTION dbo.daycount(@startday DateTime)
RETURNS Integer AS
BEGIN

DECLARE @enddate DateTime
set @enddate = GetDate()

RETURN DATEDIFF(day, @startday, @enddate)

END


keeps saying I can't use GetDate(). Can someone tell me what's wrong
with this?


--
axism





Reply With Quote
  #3  
Old   
Dave Owen
 
Posts: n/a

Default Re: GetDate() function - 07-16-2006 , 07:36 PM



Jack is correct. Nondeterministic functions CANNOT
be used within user defined functions. What might help,
though, is that the DateTime() function can be passed to
the user defined function as a parameter.

Try altering your function to accept this passed in value.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.