[Original] Simple implementation of several ORACLE functions in MYSQL

Source: Internet
Author: User
Tags mysql functions

We do not need to migrate data from ORACLE to MYSQL. Below I have written three simple MYSQL functions. For your reference.

Is it time?
 
 
  1. DELIMITER $$ 
  2.  
  3. USE `ytt`$$ 
  4.  
  5. DROP FUNCTION IF EXISTS `is_date`$$ 
  6.  
  7. CREATE DEFINER=`root`@`localhost` FUNCTION `is_date`( 
  8. f_in CHAR(19)) RETURNS TINYINT(4) 
  9. BEGIN 
  10.   -- Created by david.yang 2012/8/9. 
  11.   IF UNIX_TIMESTAMP(f_in) = 0 THEN 
  12.     RETURN 0; 
  13.   ELSE 
  14.     RETURN 1; 
  15.   END IF; 
  16. END$$ 
  17.  
  18. DELIMITER ; 
Is it a number?
 
 
  1. DELIMITER $$ 
  2.  
  3. USE `ytt`$$ 
  4.  
  5. DROP FUNCTION IF EXISTS `is_number`$$ 
  6.  
  7. CREATE DEFINER=`root`@`localhost` FUNCTION `is_number`( 
  8.     f_in VARCHAR(255)) RETURNS TINYINT(4) 
  9. label1:BEGIN 
  10.     -- Created by david.yang 2012/8/9. 
  11.     DECLARE cnt INT UNSIGNED DEFAULT 0; 
  12.     DECLARE i INT UNSIGNED DEFAULT 1; 
  13.     DECLARE j INT UNSIGNED DEFAULT 0; 
  14.     SET cnt = LENGTH(f_in); 
  15.     loop1:WHILE i < cnt 
  16.     DO 
  17.       SET j = ASCII(SUBSTR(f_in,i,1)); 
  18.       IF  j < 48 OR j > 57 THEN 
  19.         RETURN 0; 
  20.         LEAVE label1; 
  21.       END IF; 
  22.       SET i = i + 1; 
  23.     END WHILE loop1; 
  24.     RETURN 1; 
  25.   END$$ 
  26.  
  27. DELIMITER ; 
MYSQL Implementation of ORACLE to_date function.
 
 
  1. DELIMITER $$ 
  2.  
  3. USE `ytt`$$ 
  4.  
  5. DROP FUNCTION IF EXISTS `to_date`$$ 
  6.  
  7. CREATE DEFINER=`root`@`localhost` FUNCTION `to_date`( 
  8.     f_date VARCHAR(30), f_format VARCHAR(30) 
  9.     ) RETURNS VARCHAR(30) CHARSET utf8 
  10. BEGIN 
  11.     -- '20-08-2011 22:55:02', 'dd-mm-yyyy hh24:mi:ss' 
  12.     -- '02-11-2011', 'dd-mm-yyyy' 
  13.     -- Created by david.yang 2012/8/9. 
  14.     DECLARE i_year CHAR(4); 
  15.     DECLARE i_month CHAR(2); 
  16.     DECLARE i_day CHAR(2); 
  17.     DECLARE i_time CHAR(8); 
  18.     SET i_year = SUBSTR(f_date,7,4); 
  19.     SET i_month = SUBSTR(f_date,4,2); 
  20.     SET i_day = LEFT(f_date,2); 
  21.      
  22.     IF LENGTH(f_date) = 10 THEN 
  23.       SET i_time = ''; 
  24.     ELSE 
  25.       SET i_time = RIGHT(f_date,8); 
  26.     END IF; 
  27.     RETURN CONCAT(i_year,'-',i_month,'-',i_day,' ',i_time); 
  28.     END$$ 
  29.  
  30. DELIMITER ; 

This article is from "god, let's see it !" Blog, please be sure to keep this source http://yueliangdao0608.blog.51cto.com/397025/962065

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.