I want to create a function in sql but i am getting many errors : **I want to create function in sql(in SQL Server Management studio) for :**

    Input: 7589586586     Output:  (758) 958-6586
    Input: 758ABC6586     Output: (758) 222-6586  
    Input: 758ABC65       Output: Invalid Formats (like mobile keypad)

**this is my sql code which is giving many errors:**

CREATE FUNCTION fn_bhagyashreed_phonenumber(@input VARCHAR(20))
RETURNS VARCHAR(50)
BEGIN
    DECLARE @compare VARCHAR(30) = '';            
    DECLARE @cnt INT = 1;
    DECLARE @varout VARCHAR(30) = '';
    DECLARE @val VARCHAR(30) = '';
    DECLARE @Phoutput VARCHAR(50) = '';
    DECLARE @var INT;

    SET @var = LEN(@input);

    IF @var <> 10 OR @input NOT REGEXP '^[[:alnum:]]+$' THEN
        SET @Phoutput = 'Invalid Format';
    ELSE
        WHILE @cnt <= 10 BEGIN
            SET @compare = SUBSTRING(@input, @cnt, 1);
            IF @compare IN('a','b','c','2') BEGIN
                SET @val=2;
            ELSE IF @compare IN('d','e','f','3') BEGIN
                SET @val=3;
            ELSE IF @compare IN('g','h','i','4') BEGIN
                SET @val=4;
            ELSE IF @compare IN('j','k','l','5') BEGIN
                SET @val=5;
            ELSE IF @compare IN('m','n','o','6') BEGIN
                SET @val=6;
            ELSE IF @compare IN('p','q','r','s','7') BEGIN
                SET @val=7;
            ELSE IF @compare IN('t','u','v','8') BEGIN
                SET @val=8;
            ELSE IF @compare IN('w','x','y','z','9') BEGIN
                SET @val=9;
            ELSE IF @compare = '1' BEGIN
                SET @val=1;
            ELSE IF @compare = '0' BEGIN
                SET @val=0;
            END 
            SET @varout = CONCAT(@varout,@val);
            SET @cnt = @cnt + 1;
        END;
        SET @Phoutput = CONCAT('(',SUBSTRING(@varout,1,3),')',' ',SUBSTRING(@varout,4,3),'-',SUBSTRING(@varout,7,4));
    END; IF;
    RETURN Phoutput;
END$$

**these are the errors:**

Msg 102, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 13
Incorrect syntax near 'REGEXP'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 15
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 20
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 22
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 24
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 26
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 28
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 30
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 32
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 34
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 36
Incorrect syntax near the keyword 'ELSE'.
Msg 195, Level 15, State 10, Procedure fn_bhagyashreed_phonenumber, Line 39
'CONCAT' is not a recognized built-in function name.
Msg 195, Level 15, State 10, Procedure fn_bhagyashreed_phonenumber, Line 42
'CONCAT' is not a recognized built-in function name.
Msg 102, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 43
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Procedure fn_bhagyashreed_phonenumber, Line 45
Incorrect syntax near 'END$$'.

***KINDLY Help me.Its urgent.***