Avatar of GordonPrince
GordonPrince
Flag for United States of America asked on

what's wrong with this simple function

I'd like a simple, reusable way to select the date (without the time) from the server. I thought I would create a function as shown in the code window.

But the create statement throws these errors:
Msg 102, Level 15, State 1, Procedure DateFromServer, Line 3
Incorrect syntax near 'RETURNS'.
Msg 178, Level 15, State 1, Procedure DateFromServer, Line 8
A RETURN statement with a return value cannot be used in this context.

Any ideas how to fix this simple function coding error? And/or another way to accomplish what I'm trying to accomplish?
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE FUNCTION [dbo].[DateFromServer] 
RETURNS char(10)
AS  
BEGIN
  DECLARE @dat char(10)
  set @dat = (select convert(char(10), getdate(),101))
  RETURN(@dat)
END

Open in new window

Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Zberteoc

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
s_chilkury

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jorgedeoliveiraborges

CREATE FUNCTION [dbo].[DateFromServer]  ()
RETURNS char(10)

Open in new window

Vitor Montalvão

What for you are reinventing the wheel?
GETDATE() returns date from server. You can use CONVERT function to format the result to show only DATE.

Good luck
SOLUTION
Zberteoc

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
GordonPrince

ASKER
I'm just trying to not have to retype the code over and over and hope it comes out the same every time. That's all the function is for.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Zberteoc

To be honest is not that much to write to justify a function in this case. Beside that the performance will be negatively impacted by a user define function, even if not by too much.