日期:2014-05-16  浏览次数:20981 次

ORACLE的几个函数在MYSQL里面的简单实现
最忌在做ORACLE到MYSQL得迁移,以下我写了三个简单的MYSQL里面米有的函数。 供大家参考。 




判断是否为时间?
DELIMITER $$


USE `ytt`$$


DROP FUNCTION IF EXISTS `is_date`$$


CREATE DEFINER=`root`@`localhost` FUNCTION `is_date`(
f_in CHAR(19)) RETURNS TINYINT(4)
BEGIN
  -- Created by david.yang 2012/8/9.
  IF UNIX_TIMESTAMP(f_in) = 0 THEN
    RETURN 0;
  ELSE
    RETURN 1;
  END IF;
END$$


DELIMITER ;





判断是否为数字?
DELIMITER $$


USE `ytt`$$


DROP FUNCTION IF EXISTS `is_number`$$


CREATE DEFINER=`root`@`localhost` FUNCTION `is_number`(
    f_in VARCHAR(255)) RETURNS TINYINT(4)
label1:BEGIN
    -- Created by david.yang 2012/8/9.
    DECLARE cnt INT UNSIGNED DEFAULT 0;
    DECLARE i INT UNSIGNED DEFAULT 1;
    DECLARE j INT UNSIGNED DEFAULT 0;
    SET cnt = LENGTH(f_in);
    loop1:WHILE i < cnt
    DO
      SET j = ASCII(SUBSTR(f_in,i,1));
      IF  j < 48 OR j > 57 THEN
        RETURN 0;
        LEAVE label1;
      END IF;
      SET i = i + 1;
    END WHILE loop1;
    RETURN 1;
  END$$


DELIMITER ;






ORACLE to_date 函数的MYSQL实现。



DELIMITER $$


USE `ytt`$$


DROP FUNCTION IF EXISTS `to_date`$$


CREATE DEFINER=`root`@`localhost` FUNCTION `to_date`(
    f_date VARCHAR(30), f_format VARCHAR(30)
    ) RETURNS VARCHAR(30) CHARSET utf8
BEGIN
    -- '20-08-2011 22:55:02', 'dd-mm-yyyy hh24:mi:ss'
    -- '02-11-2011', 'dd-mm-yyyy'
    -- Created by david.yang 2012/8/9.
    DECLARE i_year CHAR(4);
    DECLARE i_month CHAR(2);
    DECLARE i_day CHAR(2);
    DECLARE i_time CHAR(8);
    SET i_year = SUBSTR(f_date,7,4);
    SET i_month = SUBSTR(f_date,4,2);
    SET i_day = LEFT(f_date,2);
    
    IF LENGTH(f_date) = 10 THEN
      SET i_time = '';
    ELSE
      SET i_time = RIGHT(f_date,8);
    END IF;
    RETURN CONCAT(i_year,'-',i_month,'-',i_day,' ',i_time);
    END$$


DELIMITER ;