原始表:T
字段:
Mobile,
13312345678 13512345671 13812345675
13316012347 13101234568 13812345687
13312345678 13812345675 18912345678 13312345679 13212345678
13012345679
以上是值的几种形式,
1. 所有的手机号码都是11位,并且两个号码中间是有空格区分开的
2. 有可能会存在空一个11位的,如第二行所示
3. 最大一个值也就5个号码,
4. 最小也就有一个11位的手机号码
需要得到的结果是:
Mobile, Mobile2, Mobile3, Mobile4,Mobile5
13312345678,13512345671,13812345675,NULL,NULL
13316012347,NULL,13101234568,13812345687
13312345678,NULL,13812345675,18912345678,13312345679,13212345678
13012345679,NULL,NULL,NULL,NULL
------解决方案--------------------
create table t (id int identity(1,1),mobile nvarchar(max))
insert into t values ('13312345678 13512345671 13812345675'),
('13316012347 13101234568 13812345687')
,('13312345678 13812345675 18912345678 13312345679 13212345678 '),
('13012345679')
select * from t
select substring(mobile,1,11) as mobile1,
case substring(mobile,13,11) when '' then null else substring(mobile,13,11) end as mobile2,
case substring(mobile,25,11) when '' then null else substring(mobile,25,11) end as mobile3,
case substring(mobile,37,11) when '' then null else substring(mobile,37,11) end as mobile4,
case substring(mobile,49,11) when '' then null else substring(mobile,49,11) end as mobile5,
case substring(mobile,61,11) when '' then null else substring(mobile,61,11) end as mobile6
from t
------解决方案--------------------
上面写的sql语句能行吗?
如果不行,我觉得用函数来解决比较好。
函数来处理,主要就是按照你上面的规则,对每个字段的字符串,进行循环判断。