all groups > sql server programming > march 2005 >
You're in the

sql server programming

group:

getdate in UDF


getdate in UDF Ed
3/5/2005 2:17:02 PM
sql server programming:
hi,
I am not able to use today date by using getdate() in UDF. Is there
anyway to work around?
Thanks
RE: getdate in UDF CBretana
3/5/2005 2:35:02 PM
No, you have to get the current date before you call the UDF, and pass that
value as another UDF Parameter. This is because UDFs must be "determinisitic"

[quoted text, click to view]
Re: getdate in UDF Ed
3/5/2005 2:37:03 PM

if order to use getdate() in UDF, i could not do something like
select * from dbo.returndate(getdate())
it generates an error....
or
I have to use stored procedure instead????

Ed



[quoted text, click to view]
Re: getdate in UDF Aaron [SQL Server MVP]
3/5/2005 5:22:09 PM
http://www.aspfaq.com/2439

--
http://www.aspfaq.com/
(Reverse address to reply.)




[quoted text, click to view]

Re: getdate in UDF Aaron [SQL Server MVP]
3/5/2005 6:25:37 PM
Well, what is the error message? Is your function a table-valued function,
or a user-defined function? If it is a user-defined function, use SELECT
dbo.returndate(getdate()) not SELECT * FROM ... I don't see any SELECT *
FROM examples in the link I posted.

--
http://www.aspfaq.com/
(Reverse address to reply.)




[quoted text, click to view]

Re: getdate in UDF Louis Davidson
3/5/2005 7:21:46 PM
No, they do not have to be deterministic, but you cannot use
non-deterministic system functions in user-defined functions, which
getdate() is. Any function that selects from a table or view will be
non-deterministic. You can create a view that contains getdate() and select
from it, if you want. Best to pass it as a parm though:

create function returnTime
(
@date datetime
)
returns datetime
as
begin
return @date
end
go
select dbo.returnTime(getdate())
go

returns:


------------------------------------------------------
2005-03-05 19:20:03.650

Well, if you happen to run it at the same time I did, but it will return a
data value :)

--
----------------------------------------------------------------------------
Louis Davidson - drsql@hotmail.com
SQL Server MVP

Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design -
http://www.apress.com/book/bookDisplay.html?bID=266
Blog - http://spaces.msn.com/members/drsql/
Note: Please reply to the newsgroups only unless you are interested in
consulting services. All other replies may be ignored :)
[quoted text, click to view]

Re: getdate in UDF CBretana
3/5/2005 7:25:02 PM
Yes you're right. Got that confused...

[quoted text, click to view]
Re: getdate in UDF Nigel Rivett
3/6/2005 7:59:03 AM
You can use a view with getdate() inside a udf but you are fololing the
optimiser and this can produce invalid (i.e. incorrect) results so I would
not try it and certainly not use it in a production system.

[quoted text, click to view]
Re: getdate in UDF Nigel Rivett
3/6/2005 12:50:16 PM
A simple example is

create view d
as
select d = getdate()
go

drop function x
create function x(@i int)
returns datetime
as
begin
return (select d from d)
end
go

select dbo.x(s1.id), count(*) from master..sysobjects s1,
master..sysobjects s2
group by dbo.x(s1.id)
order by dbo.x(s1.id)


Nigel Rivett
www.mindsdoor.net

*** Sent via Developersdex http://www.developersdex.com ***
Re: getdate in UDF Louis Davidson
3/6/2005 1:40:27 PM
What do you mean, invalid? I have not seen this mentioned before. I have
never used one, but have seen it mentioned before that you might get invalid
(ie wrong) results.

--
----------------------------------------------------------------------------
Louis Davidson - drsql@hotmail.com
SQL Server MVP

Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design -
http://www.apress.com/book/bookDisplay.html?bID=266
Blog - http://spaces.msn.com/members/drsql/
Note: Please reply to the newsgroups only unless you are interested in
consulting services. All other replies may be ignored :)
[quoted text, click to view]

Re: getdate in UDF Aaron [SQL Server MVP]
3/6/2005 3:14:55 PM
See the link to Tibor's explanation here:
http://www.aspfaq.com/2439

--
http://www.aspfaq.com/
(Reverse address to reply.)




[quoted text, click to view]

AddThis Social Bookmark Button