Help creating a function

Venkat Venkataramanan 76 Reputation points
2020-10-10T02:18:09.347+00:00

Hello:

I am trying to create a function:

CREATE FUNCTION [dbo].[fStringToDate]
(@pString varchar(8))
RETURNS DATE
AS
BEGIN TRY

    DECLARE @Return DATE

    Set @pString = TRIM(@pString)
    IF LEN(@pString) = 8 
        BEGIN
            Set @Return = CAST(SUBSTRING(@pString,5,2)+'/'+LEFT(@pString,2)+'/'+LEFT(@pString,4) as DATE);
    END;
    IF LEN(@pString) = 6 
        BEGIN
            Set @Return = CAST(RIGHT(@pString,2)+'/01/'+LEFT(@pString,4) AS DATE);
        END;
    IF LEN(@pString) = 4
        BEGIN
            Set @Return = CAST('01/01/'+@pString AS DATE);
        END;
    RETURN @Return;

END TRY;  

BEGIN CATCH  

    SET @Return = NULL; 
    RETURN @Return;

END CATCH; 

But I get the following error messages:

Msg 102, Level 15, State 1, Procedure fStringToDate, Line 24 [Batch Start Line 0]
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Procedure fStringToDate, Line 31 [Batch Start Line 0]
Incorrect syntax near 'CATCH'.

What am I doing wrong?

Venki

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,625 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2020-10-10T06:02:36.907+00:00

    If you want to catch the conversion problems, then consider TRY_CAST or TRY_CONVERT. In addition, fix some defects of initial approach:

    CREATE FUNCTION [dbo].[fStringToDate]
        (@pString varchar(8))
    RETURNS date
    AS
    BEGIN
        declare @return date
    
        declare @s as varchar(max) = TRIM(@pString)
    
        set @s =
            case LEN(@s)
            when 8 then 
                SUBSTRING(@s, 5, 2) + '/' + RIGHT(@s, 2) + '/' + LEFT(@s, 4)
            when 6 then
                RIGHT(@s, 2) + '/01/' + LEFT(@s, 4)
            when 4 then
                '01/01/' + @s
            end
    
        set @return = TRY_CAST(@s as date)
    
        return @return
    END
    

1 additional answer

Sort by: Most helpful
  1. Tom Cooper 8,466 Reputation points
    2020-10-10T03:43:22.647+00:00

    You cannot use TRY/CATCH in a user defined function. That is documented in
    https://learn.microsoft.com/en-us/sql/t-sql/language-elements/try-catch-transact-sql?view=sql-server-ver15
    (scroll down to the bottom of the REMARKS section).

    Errors in functions must be caught in the calling code. So put the call to your function in a TRY/CATCH block.

    Also, the function must be enclosed in a BEGIN END block.

    So your function should look like

    CREATE FUNCTION [dbo].[fStringToDate]  
    (@pString varchar(8))  
    RETURNS DATE  
    AS  
    BEGIN  
      
             DECLARE @Return DATE  
             Set @pString = TRIM(@pString)  
             IF LEN(@pString) = 8   
                 BEGIN  
                     Set @Return = CAST(SUBSTRING(@pString,5,2)+'/'+LEFT(@pString,2)+'/'+LEFT(@pString,4) as DATE);  
             END;  
             IF LEN(@pString) = 6   
                 BEGIN  
                     Set @Return = CAST(RIGHT(@pString,2)+'/01/'+LEFT(@pString,4) AS DATE);  
                 END;  
             IF LEN(@pString) = 4  
                 BEGIN  
                     Set @Return = CAST('01/01/'+@pString AS DATE);  
                 END;  
             RETURN @Return;  
              
              
             SET @Return = NULL;   
             RETURN @Return;  
              
    END  
    

    Tom

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.