mysql 创建 存储函数 split_string

1、创建函数

-- 创建存储函数
DROP FUNCTION IF EXISTS SPLIT_STR; -- 判断是否存在,存在则删除
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
) RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
   RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, ‘‘);
END;

2、调用方法

SELECT split_str(7654,7698,7782,7788,,,1) as one,
split_str(7654,7698,7782,7788,,,2) as two,
split_str(7654,7698,7782,7788,,,3) as three,
split_str(7654,7698,7782,7788,,,4) as forms

 

mysql 创建 存储函数 split_string

上一篇:SQL70004: In this context, you must specify columns by using a CREATE TABLE statement instead of by using an ALTER TABLE statement


下一篇:mysql 对于有null值的 NOT IN和NOT EXISTS