MsSQL到MySQL的功能转换

时间:2011-07-15 17:58:44

标签: mysql sql-server function

所以我试图将我在MSSQL中创建的函数转换为MYSQL。我在MSSQL中编写的方式是:

ALTER   function FormatDate(@date datetime) returns varchar(10)
begin
declare @salida varchar(10)

if (@date != '') and (@date != '01/01/1900')
    begin
        declare @day varchar(2)
        set @day = cast(day(@date) as varchar)
        if  len(@day) = 1 
                set @day = '0' + @day

        declare @month varchar(2)
        set @month = cast(month(@date) as varchar)
        if  len(@month) = 1 
                set @month = '0' + @month

        select @salida = @month + '/' + @day + '/' + cast(year(@date) as varchar)
    end
else 
    set @salida = null

return @salida
end

我正在尝试将该函数转换为MYSQL函数。我试过这个:

Delimiter $$

create function FormatDate(tiempo datetime) 
RETURNS varchar(10)
READS SQL DATA
BEGIN

declare salida varchar(10);

if ((tiempo != '') and (tiempo != '01/01/1900')) then 

        BEGIN

            declare dia varchar(2);

            set dia = cast(day(tiempo) as varchar);

            if  len(dia) = 1 then 
                set dia = '0' + dia;
            END IF;

            declare mes varchar(2);

            set mes = cast(month(tiempo) as varchar);

            if  len(mes) = 1 then 
                set mes = '0' + mes;
            END IF;

            select salida = mes + '/' + dia + '/' + cast(year(tiempo) as varchar);




    else 

        set salida = null;

     END;  End if;

        return (salida);

END $$

Delimiter ; 

但是当我尝试执行该代码时出现错误。

这是我得到的错误:

Error Code: 1064. You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'varchar);

            if  len(dia) = 1 then 
          ' at line 14
  • 有人可以帮我把这个MSSQL函数转换成MYSQL函数吗?

1 个答案:

答案 0 :(得分:0)

在MySQL中确定字符串长度的函数叫做LENGTH(),而不是len()

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length

MORE:

我在下面添加了您的功能的工作版本。但请注意,内置函数DATE_FORMAT()完全符合您的要求:

mysql> SELECT FormatDate( NOW() ), DATE_FORMAT( NOW(), "%m/%d/%Y" );
+---------------------+----------------------------------+
| FormatDate( NOW() ) | DATE_FORMAT( NOW(), "%m/%d/%Y" ) |
+---------------------+----------------------------------+
| 07/15/2011          | 07/15/2011                       |
+---------------------+----------------------------------+

您应该使用它或通过调用该函数替换您的函数体。但是,这是您的函数的MySQL兼容版本:

DELIMITER $$

CREATE FUNCTION `FormatDate`(tiempo datetime) RETURNS varchar(10) 
    READS SQL DATA
BEGIN

    DECLARE salida VARCHAR(10);
    DECLARE dia VARCHAR(2);
    DECLARE mes VARCHAR(2);

    IF ( (tiempo <> '') AND ( tiempo <> '01/01/1900' ) ) THEN

        SET dia := CAST( DAY( tiempo ) AS CHAR );
        IF LENGTH( dia ) = 1 THEN
            SET dia := CONCAT( '0', dia);
        END IF;

        SET mes := CAST( MONTH( tiempo ) AS CHAR );
        IF LENGTH( mes ) = 1 THEN
            SET mes := CONCAT( '0', mes );
        END IF;

        SET salida := CONCAT_WS( '/', mes, dia, CAST( YEAR( tiempo ) AS CHAR ) );
    ELSE
        SET salida := NULL;
    END IF;

    RETURN salida;

    END $$

DELIMITER ;