大家都知道,Oracle,PostgreSQL等数据库中有将所有单词首字母变大写的功能函数initcap,但是MySQL没有,今天写了一个。分享:
- delimiter //
- drop function initcap//
- create function initcap(ss varchar(1000)) returns varchar(1000)
- begin
- declare lena int;
- declare pos int;
- declare firsta char(1);
- declare seconda varchar(999);
- declare tmpstr varchar(1000);
- declare retstr varchar(1000);
- if (length(trim(ss)) = 0) then
- return '';
- end if;
- if (right(ss,1) != ' ') then
- set ss=concat(ss,' ');
- end if;
- set pos=instr(ss,' ');
- set lena=length(ss);
- while (pos > 0) do
- set tmpstr=trim(left(ss,pos));
- set ss = right(ss,lena - pos );
- set lena = length(tmpstr);
- set firsta = upper(left(tmpstr,1));
- set seconda = lower(right(tmpstr,lena - 1));
- if (length(retstr) > 0) then
- set retstr = concat(retstr,' ',firsta,seconda);
- else
- set retstr = concat(firsta,seconda);
- end if;
- set pos = instr(ss,' ');
- set lena = length(ss);
- end while;
- return retstr;
- end;
- //
- delimiter ;
阅读(7869) | 评论(0) | 转发(0) |