zoukankan      html  css  js  c++  java
  • mysql数据库函数(将中文大写数字转换为阿拉伯数字)

    还是响应公司的业务需求,在做统计表的时候需要对数字进行运算,可是由于前人的偷懒给我造成了很大的麻烦(居然将大写数字直接存入,没有阿拉伯数字,晕~);所以啊,我抱怨也没啥用,而且去网上也没有找到将中文大写数字转换为阿拉伯数字的mysql函数,没办法,没有轮子就只好造轮子啦!

    话不多说,上代码:

    CREATE DEFINER = 'root'@'localhost'
    FUNCTION myhouse.change_to_num(cn_num varchar(255))
      RETURNS int(11)
    begin
    	declare i int default 1;
    	declare result int default 0;
    	declare str varchar(10);
    	declare num int;
    	declare temp int default 1;
    	while i <= char_length(cn_num) do
    		set str = substring(cn_num, i, 1);
    		set num = case str
    								when '零' then 0
    								when '壹' then 1
    								when '贰' then 2
    								when '叁' then 3
    								when '肆' then 4
    								when '伍' then 5
    								when '陆' then 6
    								when '柒' then 7
    								when '捌' then 8
    								when '玖' then 9
    								when '拾' then 10
    								when '佰' then 100
    								when '仟' then 1000
                    ELSE 0
    							end;
    		if num!=10 && num!=100 && num!=1000 then
    			set temp = num;
          IF i=char_length(cn_num) THEN
            SET result = result + temp;
          END IF;
    		else
    			set result = result + (temp * num);
    		end if;
    		set i = i + 1;
    	end while;
    	return result;
    end
    

      我这个不能保证适用于所有转换,而且可能还有隐藏bug。。。因此仅供参考哈

  • 相关阅读:
    前端知识体系
    DOMContentLoaded与load的区别
    最佳网页宽度及其实现
    一些颜色工具网站
    Firebug入门指南
    CSS中背景图片定位方法
    字符编码笔记:ASCII,Unicode 和 UTF-8
    学JS的书籍
    深入理解定位父级offsetParent及偏移大小
    event——事件对象详解
  • 原文地址:https://www.cnblogs.com/CodeBunny/p/13097514.html
Copyright © 2011-2022 走看看