Link to home
Create AccountLog in
Avatar of GordonPrince
GordonPrinceFlag 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

ASKER CERTIFIED SOLUTION
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
CREATE FUNCTION [dbo].[DateFromServer]  ()
RETURNS char(10)

Open in new window

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
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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.
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.