Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Retrieving GETDATE within a UDF

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2005-05-25 : 12:11:57
T-SQL doesn't allow calling GETDATE() within a function.

I'm going to have to code it to accept @GetDate a parameter (no big deal), but finding and modifying all the points that call the function is a lot of work.

Anyone have an out that'll give me access to the date from within a function?

Sam

ajthepoolman
Constraint Violating Yak Guru

384 Posts

Posted - 2005-05-25 : 12:46:17
The parameter method is the only way I have found to get around it.

I have tried
GETDATE()
dbo.GETDATE()

and other ways, but you simply cannot do it. Some of the smarter people on this forum can explain it better, but in a nutshell SQL does not support the use of SOME functions within UDFs.

Aj
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-05-25 : 12:56:28
YOu can use the same idea I used here:

http://weblogs.sqlteam.com/jeffs/archive/2004/11/22/2927.aspx

only use GetDate() instead of Rand().

Check BOL, the blog and the comments for some info about why you cannot use certain functions in a UDF.

- Jeff
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-05-25 : 13:04:55
Schweet !

Thanks Jeff,

Sam
Go to Top of Page

ajthepoolman
Constraint Violating Yak Guru

384 Posts

Posted - 2005-05-25 : 13:58:27
Great article Jeff! I suppose that work around could be used for just about any problem you have with UDF limitations.

Aj
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-05-26 : 01:25:26
A bit tangential 'coz clearly Jeff's solution rocks, but I create a new function MyFunction_V2 when I need to add a parameter, and then I can modify the code piecemeal (assuming the old calls are still valid, up to a point).

You can also DROP the "V1" Function to force errors from old code (obvious an additional parameter requirement is going to create an error in old code! but its applicable to other circumstances where the new version now requires no code to be calling the old version - perhaps you've changed the basis of a return value, or somesuch.

Kristen
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-05-26 : 02:08:09
quote:
Originally posted by jsmith8858

YOu can use the same idea I used here:

http://weblogs.sqlteam.com/jeffs/archive/2004/11/22/2927.aspx

only use GetDate() instead of Rand().

Check BOL, the blog and the comments for some info about why you cannot use certain functions in a UDF.

- Jeff



http://weblogs.sqlteam.com/davidm/archive/2003/10/28/378.aspx





Damian
Ita erat quando hic adveni.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-05-26 : 09:01:30
D'oh !! Sorry David! I will add a little note to the beginning of my post ...

However -- I can't help resist something I had noticed a few months ago when cleaning up the spam from my blog:

http://weblogs.sqlteam.com/jeffs/archive/2003/09/30/195.aspx

http://www.sqlteam.com/item.asp?ItemID=15044



I guess it happens! I think we all spend a *little* too much time together here at sqlteam !





- Jeff
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-05-26 : 19:07:48
oops, you got me. Hadn't even noticed that blog.

I think you're right.

I have a bit of a grand scheme to rebuild SQLTeam so that blog articles can be front page articles, unify the lot... But that spare time thing is a bitch



Damian
Ita erat quando hic adveni.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2005-05-26 : 19:18:37
Where reasonable people here.. So here is a solution.

I sue Jeff, Jeff sues Damian, Damian sues me and then get we together and kill the lawyers.

And Damian, if you think you are stretched for time now..wait till junior comes along!

DavidM

A front-end is something that tries to violate a back-end.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-05-26 : 19:27:37
It's my thread. I should sue all you guys. I just don't know what for.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2005-05-26 : 20:07:59
LOL Sam! Does it matter?

DavidM

A front-end is something that tries to violate a back-end.
Go to Top of Page

ajthepoolman
Constraint Violating Yak Guru

384 Posts

Posted - 2005-05-26 : 20:27:02
You can sue me for having the least number of posts of all the people who posted here.

Aj
Go to Top of Page
   

- Advertisement -