Mysql常用函数

1 ora_date_to_char:实现to_char转换日期的功能

 

参数说明:1)入参:时间,格式

 

2)出参:时间格式字符串

 

例如:SELECT ora_date_to_char(a.oper_time,'yyyy-mm-dd') FROM sp_issue a; 

DELIMITER $$
USE `issue`$$
DROP FUNCTION IF EXISTS `ora_date_to_char`$$
CREATE DEFINER=`root`@`%` FUNCTION `ora_date_to_char`(in_date TIMESTAMP,
                        in_format VARCHAR(50)) RETURNS VARCHAR(50) CHARSET utf8
BEGIN
    DECLARE out_format  VARCHAR(50);
    DECLARE v_length    TINYINT;
    SET v_length=LENGTH(in_format);
    SET in_format=UPPER(in_format); 
    SET in_format=REPLACE(in_format,'YYYY','%Y');
    SET in_format=REPLACE(in_format,'YY','%y');
    SET in_format=REPLACE(in_format,'MM','%m');
    SET in_format=REPLACE(in_format,'DD','%d');
    SET in_format=REPLACE(in_format,'HH24','%H');
    SET in_format=REPLACE(in_format,'MI','%i');
    SET in_format=REPLACE(in_format,'SS','%s');
    SET in_format=REPLACE(in_format,'MONTH','%b');
    SET in_format=REPLACE(in_format,'DAY','%a'); 
    SET out_format=DATE_FORMAT(in_date,in_format);
    RETURN out_format;
END$$
DELIMITER ;

 

2.p_f_getnum:获取字符串个数

参数说明:1)入参:源字符串,目标字符串

2)出差:目标字符串个数

例如:SELECT p_f_getnum('aaabbbcccaaa','aaa') FROM DUAL; --返回2

 

DELIMITER $$

USE `issue`$$

DROP FUNCTION IF EXISTS `p_f_getnum`$$

CREATE DEFINER=`root`@`%` FUNCTION `p_f_getnum`(in_str VARCHAR(4000),in_div VARCHAR(10)) RETURNS SMALLINT(6)
BEGIN
    DECLARE v_num SMALLINT;
    DECLARE v_length SMALLINT;
    DECLARE v_length2 SMALLINT;
    DECLARE v_div_length SMALLINT;
     SET v_length=CHAR_LENGTH(in_str);
     SET v_length2=CHAR_LENGTH(REPLACE(in_str,in_div,''));
     SET v_div_length=CHAR_LENGTH(in_div);
     SET v_num=(v_length-v_length2)/v_div_length;
     RETURN v_num;
  END$$

DELIMITER ;

 

 

3 p_f_getstr:从in_str取第in_int个被in_fgr分割的字符串

 

参数说明:1)入参:源字符串,分割字符串,N

 

2)出参:截取后的字符串

 

当位置入参=-1时,截取最后一个字符串

 

 

 

例如:

 

SELECT p_f_getstr('obs/oby/obj','/',2) FROM DUAL; --返回oby

 

SELECT p_f_getstr('obs/oby/obj','ob',3) FROM DUAL;-- 的结果是'y/'

 

SELECT p_f_getstr('aaa/bbb/ccc','/',-1) FROM DUAL; 结果是ccc

DELIMITER $$

USE `issue`$$

DROP FUNCTION IF EXISTS `p_f_getstr`$$

CREATE DEFINER=`root`@`%` FUNCTION `p_f_getstr`(in_str VARCHAR(4000),in_fgf VARCHAR(10),in_int TINYINT) RETURNS VARCHAR(2000) CHARSET utf8
BEGIN
    DECLARE v_str VARCHAR(4000);
    DECLARE n_str SMALLINT;
    DECLARE n_fgf TINYINT;
    DECLARE n_1   SMALLINT;
    DECLARE n_2   SMALLINT;
    DECLARE v_r   VARCHAR(4000);
     SET v_str=in_str;
     SET n_str=CHAR_LENGTH(in_str);
     SET n_fgf=CHAR_LENGTH(in_fgf);
     
     IF ora_f_instr(v_str,in_fgf,-1,1)<>CHAR_LENGTH(v_str)-n_fgf+1 THEN
      SET v_str = CONCAT(in_str,in_fgf);
     END IF;
     IF in_int > 1 THEN
         SET   n_1 = ora_f_instr(v_str, in_fgf, 1, in_int - 1);
         SET   n_2 = ora_f_instr(v_str, in_fgf, 1, in_int);
         SET   v_r = SUBSTR(v_str, n_1 + n_fgf, n_2 - n_1 - n_fgf);
     END IF;
     
     IF in_int = 1 THEN
            SET v_r = SUBSTR(v_str, 1, ora_f_instr(v_str, in_fgf,1,1) - 1);
     END IF;
     
     IF in_int = 0 OR in_int<-1 THEN
            SET v_r ='';
     END IF;
     
     IF in_int = -1 THEN
            SET v_r = REPLACE(SUBSTR(v_str,ora_f_instr(v_str, in_fgf,-1,2)),in_fgf,'');
     END IF;
  
        RETURN v_r;
  END$$

DELIMITER ;

 

 

4 p_f_instr:返回str中第in_pos位置后,subStrtimes出现在str的哪个位置上

参数说明:1)入参:源字符串,目标字符串,源字符串pos位置,目标字符串出现次数

  2)出参:目标字符串出现N次的位置

例如:

SELECT p_f_instr('obs/oby/obj','/',1,2) FROM DUAL; --返回:8

SELECT p_f_instr('obs/oby/obj','ob',1,2) FROM DUAL; --返回:5

 

DELIMITER $$

USE `issue`$$

DROP FUNCTION IF EXISTS `p_f_instr`$$

CREATE DEFINER=`root`@`%` FUNCTION `p_f_instr`(in_str VARCHAR(4000),in_div VARCHAR(10),in_pos SMALLINT,in_nth SMALLINT) RETURNS SMALLINT(6)
BEGIN
     DECLARE out_pos      SMALLINT;
    DECLARE v_length     SMALLINT;
    DECLARE v_div_length SMALLINT;
    DECLARE v_pos        SMALLINT;
    SET v_pos=0;
    SET v_length=CHAR_LENGTH(in_str); 
    SET v_div_length=CHAR_LENGTH(in_div); 
     IF in_pos>0 THEN
       IF in_nth>1 THEN
       WHILE  in_nth>1  DO
         IF   LOCATE(in_div,in_str,v_pos+1)>0 THEN
         SET v_pos=LOCATE(in_div,in_str,v_pos+1)+1;
         END IF;
         SET in_nth=in_nth-1;
       END WHILE;
       END IF;
        SET out_pos=LOCATE(in_div,in_str,in_pos+v_pos);
     END IF;
     
     IF in_pos<0 THEN 
       IF in_nth<=(v_length-CHAR_LENGTH(REPLACE(in_str,in_div,'')))/v_div_length THEN
       SET in_nth=(v_length-CHAR_LENGTH(REPLACE(in_str,in_div,'')))/v_div_length-in_nth+2;
       ELSE
        SET out_pos=0;
        RETURN out_pos;
       END IF;
      
       WHILE  in_nth>1  DO
         IF   LOCATE(in_div,in_str,v_pos+1)>0 THEN
         SET v_pos=LOCATE(in_div,in_str,v_pos+1)+1;
         END IF;
         SET in_nth=in_nth-1;
       END WHILE;
       END IF;
      
       SET out_pos=LOCATE(in_div,in_str,in_pos+v_pos);
    RETURN out_pos;
END$$

DELIMITER ;

 

 

 

5 CONCAT:可以连接多个字符串

例如:

SELECT CONCAT('a','b','c','d','e') FROM DUAL; --返回’abcde’

 

 

6 GROUP_CONCAT:反向应用实现,实现列转行

SELECT GROUP_CONCAT(code_name) FROM sp_code a WHERE a.type_code = 'ITEM_CODE' AND code_value NOT LIKE '6%'; -- 返回补丁包的所有环节

7 日期格式

7.1 STR_TO_DATE:字符串转日期格式

例如:

SELECT STR_TO_DATE(NOW(),'%Y-%m-%d') FROM DUAL; -- 返回当天日期字符串2014-11-11

SELECT STR_TO_DATE(NOW(),'%Y-%m-%d %H:%i:%s')FROM DUAL;

7.2 DATE_FORMAT 格式化日期,常与字符串比较

SELECT * FROM sp_process a WHERE DATE_FORMAT(a.end_time, '%Y%m%d') <= '20141228'; -- 格式化日期与字符串比较

 

 

 

8 当前时间获取

 

MySQL获取系统当年时间sysdate()now()

 

SELECT SYSDATE() FROM DUAL;

 

SELECT NOW() FROM DUAL;

 

 

 

9 Locate instr substr

 

Locate(substr, str, pos),返回字符串strpos个位置后substr的位置,相当于oracleinstr

 

instr(instr , substr),返回字符串substrstr中第一次出现的位置

 

substr(str, start, end),截取字符串从startend

 

SELECT SUBSTR('abcdefg',LENGTH('abcdefg'),1); -- 获取字符串最后一位,返回’g’

 

 

 

10 IFNULL函数

 

例如:SELECT IFNULL(1,0) FROM DUAL; --返回1

 

SELECT IFNULL(0,10) FROM DUAL; --返回0

 

SELECT IFNULL(1/0,10) FROM DUAL; --返回10

 

SELECT IFNULL('',10) FROM DUAL; --返回''

 

SELECT IFNULL(NULL,10) FROM DUAL; --返回10

 

 

 

11case when 语句: 替代Oracledecode语句

 

SELECT  (CASE

 

      a.type_process

 

      WHEN '888'

 

      THEN '运维流程'

 

      WHEN '887'

 

      THEN '补丁包流程'

 

      ELSE '其他'

 

    END

 

  ) 类型

 

FROM

 

  sp_issue a

 

LIMIT 0, 100;

 

12 MySQL datetime格式时间相减没有意义

TIME_TO_SEC(TIMEDIFF())

SELECT DATE_SUB(CURDATE(),INTERVAL 3 DAY); -- 获取3天前的日期

 

posted @ 2020-07-08 16:19  林被熊烟岛  阅读(163)  评论(0编辑  收藏  举报