最忌在做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 ;
本文出自 “上帝,咱們不見不散!” 部落格,請務必保留此出處http://yueliangdao0608.blog.51cto.com/397025/962065