Solved

Problem using GETDATE() in UDF function

Posted on 2006-07-04
7
408 Views
Last Modified: 2012-05-05
Hello,

We need a UDF function that send the following :

CREATE function dbo.osintNow()
RETURNS int
AS
BEGIN

DECLARE @iResult int

SET @iResult = 10000 * day(dbo.GETDATE())

RETURN @iResult
END

We can create it correctly but when we want to use it, we get :

Invalid object name 'dbo.GETDATE'.

Why ???
0
Comment
Question by:javilmer
7 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 17037924
try ...
basically getdate() doesn't have a dbo... its a system function...

CREATE function dbo.osintNow()
RETURNS int
AS
BEGIN

DECLARE @iResult int

SET @iResult = 10000 * day(GETDATE())

RETURN @iResult
END
0
 
LVL 3

Expert Comment

by:mte01
ID: 17037927
>>javilmer

Use:
SET @iResult = 10000 * day(GETDATE())
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 17037929
Hi javilmer,

You can't directly use GETDATE() inside a function, the better otpion is to pass it as an argument

CREATE function dbo.osintNow(
@today smalldatetime
)
RETURNS int
AS
BEGIN

DECLARE @iResult int

SET @iResult = 10000 * day(@today)

RETURN @iResult
END

GO

DECLARE @today smalldatetime
SET @Today = Getdate()
SELECT dbo.osintNow(@today)



Aneesh R!
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 17037941
you may find it appropriate to

create view Today
 as
  Select Getdate() as Now
           ,Day(getdate() as DayNumber
           
go

and use the view in your queries instead...

 
0
 

Author Comment

by:javilmer
ID: 17037972
yes, we added dbo.GETDATE() because a simple GETDATE leads to a syntax error
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17037975
Check my previous post and 'LowfatSpread's post'
0
 
LVL 4

Expert Comment

by:kenpem
ID: 17038096
SQL will not allow you to use GETDATE in a user-defined function.

I bumped heads with this one for ages, before finally coming up with this:

CREATE FUNCTION [dbo].[Now]() RETURNS DATETIME AS
BEGIN
    DECLARE @dt DATETIME
    SELECT @dt = dt  FROM OPENQUERY ( ***SERVERNAME***,  'SELECT dt = GETDATE()' )
    RETURN @dt
END

Now whenever you need the current timestamp within a function, use "dbo.Now()" instead of "GETDATE()".
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question