Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getdate() in a function

Status
Not open for further replies.
Jun 27, 2001
837
US
I have the function below, can you not use getdate() in a function. It tells me Invalid use of 'getdate' within a function.

alter function [dbo].[fnGetOrderAlertsandMessages](@orderid int )
returns varchar(350)
as
begin
declare @ret varchar(350)
select @ret = coalesce(@ret+', ', '') +
case t.FieldValue
when '1' then oa.comments
else t.longdescr
end
from OrderAlerts oa inner join
Translate t on t.TranslateID = oa.TranslateID and t.MaxEffDtFlag = 1 and t.IsActive = 1
where oa.OrderID = @orderid
and t.FieldName in ('Alerts','Messages')
and oa.created < getdate() + 1
group by t.LongDescr, oa.comments, t.fieldvalue
return @ret
end
 
that will wotk in SQL 2005 but not in 2000 because getdate() is non deterministic

modify your function as follows
Code:
alter   function [dbo].[fnGetOrderAlertsandMessages](@orderid int ,@d datetime)
returns varchar(350)
as
begin
    declare        @ret varchar(350)
    select        @ret = coalesce(@ret+', ', '') +  
      case        t.FieldValue
         when    '1' then oa.comments
         else    t.longdescr
      end
    from        OrderAlerts oa inner join
                Translate t on t.TranslateID = oa.TranslateID and t.MaxEffDtFlag = 1 and t.IsActive = 1
    where        oa.OrderID = @orderid
    and            t.FieldName in ('Alerts','Messages')
    and oa.created < @d + 1
    group by    t.LongDescr, oa.comments, t.fieldvalue
    return        @ret
end

select dbo.fnGetOrderAlertsandMessages (1,getdate())

here is a quick example

Code:
create   function [dbo].[fnGetOrderAlertsandMessages2](@orderid int ,@d datetime)
returns datetime
as
begin
     declare       @ret datetime
    select  @ret = @d + 1
   
    return        @ret
end


select dbo.fnGetOrderAlertsandMessages2 (1,getdate())

Denis The SQL Menace
SQL blog:
 
Normally, no. I think SQL Server 2005 now allows you to use GetDate() in a function. With SQL 2000, you can 'trick' it by creating a view that returns the value from GetDate(), and then use that within the function.

First, Create the view

Code:
Create View View_GetDate
As
Select GetDate() As CurrentDate

Then, modify the function to use the view.

Code:
alter   function [dbo].[fnGetOrderAlertsandMessages](@orderid int )
returns varchar(350)
as
begin
    [!]declare @CurrentDate DateTime
    Select  @CurrentDate = (Select * From View_GetDate)   
    [/!]
    declare        @ret varchar(350)
    select        @ret = coalesce(@ret+', ', '') +  
      case        t.FieldValue
         when    '1' then oa.comments
         else    t.longdescr
      end
    from        OrderAlerts oa inner join
                Translate t on t.TranslateID = oa.TranslateID and t.MaxEffDtFlag = 1 and t.IsActive = 1
    where        oa.OrderID = @orderid
    and            t.FieldName in ('Alerts','Messages')
    and oa.created < [!]@CurrentDate[/!] + 1
    group by    t.LongDescr, oa.comments, t.fieldvalue
    return        @ret
end

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
i thought about the view but saw some responses that it would give inconsistent results
 
Tim,

I see what you mean. Functions are evaluated for each row, so the GetDate() (which includes a time component) can be different for each call to the function.

I suspect that in this case, it would be safe to use the view method because you are filtering on GetDate() + 1. In fact, I suspect that what you really want is to compare the dates, without regard to the time, which is why you are using GetDate() + 1.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George,
Code:
Select  @CurrentDate = (Select * From View_GetDate)
Why can't you use:
Code:
Set  @CurrentDate = GetDate()

pamela
 
Denis,

Can you use a non-deterministic function in a procedure?

pamela
 


You cannot use non-deterministic function is a user defined fuction. You can use them in a procedure. In this case, Tim is creating a UDF (user defined function) to concatenate a series of values in to a comma delimited string. In my opinion, this is one of the few acceptable uses for a UDF. UDF's can be problematic because of this non-deterministic issue and also for speed issues because UDF's are evaluated for each record returned by the query. This decidedly non-set based behavior tends to cause performance issues.

I've said it before, and I'll say it again. The only time I will use a function is if:

1. I want to hide complexity.
2. I want to re-use the code.

If both criteria are met, then I will use a UDF. Otherwise, the code will go in to a stored procedure.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top