/* 1.当为5位数字时,截取前三位
2.当为4位数字时:截取前两位
3.当第一位为中文后面为数字时,截取前三位
4.当为两位数字时,就要这两位*/
其实这个没有闭环,如果五位以上的数字怎么办,四位数字加字符怎么处理...
以下是SQL处理方法,使用正则表达式加case when
drop table if exists temp_20221202;
create table temp_20221202
(char_name varchar(30));
insert into temp_20221202
(char_name) values ('西1208');
insert into temp_20221202
(char_name) values ('1802');
insert into temp_20221202
(char_name) values ('23');
insert into temp_20221202
(char_name) values ('12345');
insert into temp_20221202
(char_name) values ('12345b');
select char_name
,case when (char_name REGEXP '[^0-9.]')=0 then -- 纯数字
case when length(char_name)='5' then substr(char_name,1,3) -- 五位数字
when length(char_name)='4' then substr(char_name,1,2) -- 四位数字
when length(char_name)='2' then char_name end -- 二位数字
when (substr(char_name,1,1) REGEXP "[u0391-uFFE5]")=0 -- 首字母中文
and (substr(char_name,2) REGEXP '[^0-9.]')=0 then-- 且第二位开始纯数字
substr(char_name,1,3)
end
from temp_20221202
