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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Old Forums
 CLOSED - General SQL Server
 GetDate
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

antonio@compuconsult.net
Starting Member

8 Posts

Posted - 08/04/2003 :  00:12:41  Show Profile
Hello All,

Ok I thought this would be simple.

CREATE FUNCTION [dbo].[Accounting_CalculateDaysOverDuel] (@tempDate datetime)
RETURNS int
AS
BEGIN
DECLARE @DOD int

IF DATEDIFF(day, @tempDate, getdate()) - 30 < 0
BEGIN
SET @DOD = 0
END

SET @DOD = (DATEDIFF(day, @tempDate, GETDATE())) - 30

RETURN @DOD
END

All I want to due is get the days overdue if I pass this function a date. But I keep getting

"Invalid use of getdate within a function"

What am I doing wrong?

Tony

Merkin
Funky Drop Bear Fearing SQL Dude!

Australia
4970 Posts

Posted - 08/04/2003 :  00:24:43  Show Profile  Visit Merkin's Homepage
Hi

In BOL under "User Defined Functions"

quote:

Built-in functions that can return different data on each call are not allowed in user-defined functions




Maybe you could pass the current date into the function as a parameter.

Damian
Go to Top of Page

nr
SQLTeam MVY

United Kingdom
12543 Posts

Posted - 08/04/2003 :  02:56:36  Show Profile  Visit nr's Homepage
Basically getdate() is not deterministic.
It is possible to call getdate() from a function by tricking the server but this will cause the optimiser to give an incorret plan (it expects the function to be deterministic) and queries to give incorrect results.

As Merkin says you have to pass the value to the function.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000