当前位置:首页 > PHP教程 > PHP总结归纳

MySql判断汉字、日期、数字的函数

几个平常用的mysql函数 /***************************************************** 1.判断字符串是否为汉字 返回值:1-汉字 0-非汉字 *****************************************************/ drop function if exists fc_is_hanzi; create function fc_is_h

  几个平常用的mysql函数

  /*****************************************************

  1.判断字符串是否为汉字 返回值:1-汉字 0-非汉字

  *****************************************************/

  drop function if exists fc_is_hanzi;

  create function fc_is_hanzi(

  p_str varchar(1024)

  )

  returns int(11)

  not deterministic

  sql security definer

  comment '检查字符串是否为汉字'

  begin

  /*检查字符串是否为汉字 返回值:1-汉字 0-非汉字*/

  declare _ret, i, other_cnt, l_acode int default 0;

  set _ret = 0;

  set i = 1;

  set other_cnt = 0;

  set l_acode = 0;

  while i <= char_length(p_str) do

  set l_acode = ascii(substring(p_str, i, 1));

  if l_acode<124 or l_acode>254 then

  set other_cnt = other_cnt + 1;

  end if;

  set i = i + 1;

  end while;

  if other_cnt = 0 then

  set _ret = 1;

  else

  set _ret = 0;

  end if;

  return _ret;

  end;

  /*****************************************************

  2.判断日期格式是否正确(返回值:1-正确 0-错误)

  *****************************************************/

  drop function if exists fc_ck_date;

  create function fc_ck_date(

  p_cont char(32)

  )

  returns tinyint(4)

  not deterministic

  sql security definer

  comment '判定日期格式是否正确'

  begin

  /*判定日期格式是否正确(返回值:1-正确 0-错误)*/

  /*输入值格式为:yyyymmdd 或 yyyy-mm-dd*/

  if(select date_format(p_cont,'%y%m%d')) is null then

  return 0;

  else

  return 1;

  end if;

  end;

  /*****************************************************

  3.判断字符串是否为纯数字(返回值:1-为纯数字 0-非纯数字)

  *****************************************************/

  drop function if exists fc_is_num;

  create function fc_is_num(

  p_string varchar(32)

  )

  returns int(4)

  not deterministic

  sql security definer

  comment '检查字符串是否为纯数字'

  begin

  /*检查字符串是否为纯数字*/

  /*返回值:1-为纯数字 0-非纯数字*/

  declare iresult int default 0;

  select p_string regexp '^[0-9]*$' into iresult;

  if iresult = 1 then

  return 1;

  else

  return 0;

  end if;

  end;

.syntaxhighlighter{padding-top:20px;padding-bottom:20px;}
【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!