表结构如下表名是 jsptab 列名是 na 规格列名是 gg可口可乐500ml NULL
500ml可口可乐 NULL
旺仔QQ糖32g NULL
可比克35g薯片 NULL
晨奇中性笔GP-148 NULL
14BY文具盒 NULL
2.5kg白糖 NULL
晨奇148象皮擦' 想要的结果是列名是 na 规格列名是 gg可口可乐500ml 500ml
500ml可口可乐 500ml
旺仔QQ糖32g 32g
可比克35g薯片 35g
晨奇中性笔GP-148 NULL
114BY文具盒 NULL
2.5kg白糖 2.5kg
晨奇148象皮擦 null
insert into jsptab(na) select '可口可乐500ml'
insert into jsptab(na) select '500ml可口可乐'
insert into jsptab(na) select '旺仔QQ糖32g'
insert into jsptab(na) select '可比克35g薯片'
insert into jsptab(na) select '晨奇中性笔GP-148'
insert into jsptab(na) select '14BY文具盒'
insert into jsptab(na) select '2.5kg白糖'
insert into jsptab(na) select '晨奇148象皮擦'
go
;with c1 as(
select
(case when charindex('ml',na)>0 then charindex('ml',na)
when charindex('kg',na)>0 then charindex('kg',na)
when charindex('g',na)>0 then charindex('g',na)
else 0 end)n,
(case when charindex('ml',na)>0 then 'ml'
when charindex('kg',na)>0 then 'kg'
when charindex('g',na)>0 then 'g'
else '' end)nt,na
from jsptab
),c2 as(
select a.n,max(b.number)nu,a.na,a.nt from c1 a,master..spt_values b where b.type='p' and isnumeric(substring(na,n-number,1))=1 group by a.n,a.na,a.nt
)update t set gg=substring(b.na,b.n-b.nu,b.nu)+b.nt from jsptab t inner join c2 b on t.na=b.na
select * from jsptab
/*
na gg
-------------------- ----------
可口可乐500ml 500ml
500ml可口可乐 500ml
旺仔QQ糖32g 32g
可比克35g薯片 35g
晨奇中性笔GP-148 NULL
14BY文具盒 NULL
2.5kg白糖 2.5kg
晨奇148象皮擦 NULL(8 行受影响)*/
go
drop table jsptab
服务器: 消息 208,级别 16,状态 1,行 1
对象名 'c2' 无效。