开发者

Calling a custom SQL function

This feels like a basic question, but I have tried everything! I'm writing a custom function SQL function to convert integers into dates:

CREATE FUNCTION convert_to_date (@fin INT)
RETURNS DATE
AS
BEGIN
    DECLARE @fout DATE
    SET @fout = CASE WHEN @fin IN (开发者_如何学Go'','99999999','0','1') THEN NULL
        ELSE CONVERT(DATE,CAST(@fin AS CHAR(8)))
        END
    RETURN @fout
END

SELECT dbo.convert_to_date(DtSurgDischarge) AS DischargeDate
FROM [TR_MASTER.registry].[dbo].[mgh_tumor]

I get the following error message:

Msg 156, Level 15, State 1, Procedure convert_to_date, Line 16 Incorrect syntax near the keyword 'SELECT'.

What am I doing wrong? Feels like it must be obvious. Thanks everyone!


At the very least you need a GO after the END statement of the function, if you're attempting to execute both the function create and the SELECT in one script - assuming you're using SSMS to run the script, that is.

CREATE FUNCTION convert_to_date (@fin INT)
RETURNS DATE
AS
BEGIN
    DECLARE @fout DATE
    SET @fout = CASE WHEN @fin IN ('','99999999','0','1') THEN NULL
        ELSE CONVERT(DATE,CAST(@fin AS CHAR(8)))
        END
    RETURN @fout
END

GO

SELECT dbo.convert_to_date(DtSurgDischarge) AS DischargeDate
FROM [TR_MASTER.registry].[dbo].[mgh_tumor]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜