mysql中的制表符替换

-- 问题重现
set @v_t='    ';
select hex(@v_t)
-- 原因:    
-- ASCII格式的hex进展ox09对应char为(ht)制表tab,喔原来是有很多的制表符吧.

-- 数据库中去掉的办法
set @v_t='    ';
select f_sql_clearspace('    ');

-- java中的解决方法:
replace("\\t","")

 

CREATE DEFINER=`root`@`%` FUNCTION `f_sql_clearspace`(v_sql varchar(4000)) RETURNS varchar(4000) CHARSET utf8
begin
  declare v_tmps varchar(4000) default '';
  declare v_last varchar(2) default '';
  declare v_curr varchar(2) default '';
  declare v_num int default 0;
  declare v_max int default 0;
  
  set v_max = length(v_sql);
  while v_num <> v_max do
    set v_num = v_num + 1;
    set v_curr = substr(v_sql, v_num, 1);
   
    if (v_curr = char(9) or v_curr = char(10)) then
      set v_curr = char(32);
    end if;
   
    if (v_curr <> char(32) and (v_last is null or v_last <> char(32))) then
      set v_tmps = concat(v_tmps, v_curr);
      set v_last = v_curr;
    elseif (v_last <> char(32) and v_curr = char(32)) then
      set v_tmps = concat(v_tmps, v_curr);
      set v_last = v_curr;
    elseif (v_last = char(32) and v_curr <> char(32)) then
      set v_tmps = concat(v_tmps, v_curr);
      set v_last = v_curr;
    end if;
  end while;
  
  return v_tmps;
end

 

你可能感兴趣的:(mysql)