求高手解决sqlserver 提取字符问题!!!!
从entity_index 这张表的dwgmc这一列中提取图中画出椭圆形的一连串数字(档号)如果没有档号的可以忽略。求高手帮忙急用!!!
[最优解释]
declare @t table (dwgmc varchar(48))
insert into @t
select '011-钢梯T6-091(0600-8022-501)[td-tl]{asdfsdf}' union all
select '011-钢梯T6-091(0600-8022-505)[td-tl213]{asdfsdf}' union all
select '011-钢梯T6-091[td-tl1231]{asdfsdf}' union all
select '011-钢梯T6-091(0600-8022-501)[td-tl123]{asdfsdf}'
select
case when charindex('(',dwgmc)>0 and
charindex(')',dwgmc)>0
then
substring(dwgmc,charindex('(',dwgmc)+1,charindex(')',dwgmc)-charindex('(',dwgmc)-1)
else '无' end as dwgmc
from @t
/*
dwgmc
------------------------------------------------
0600-8022-501
0600-8022-505
无
0600-8022-501
*/
select
case when charindex('(',dwgmc)>0 and charindex(')',dwgmc)>0 and charindex('(',dwgmc)<charindex(')',dwgmc)
then
substring(dwgmc,charindex('(',dwgmc)+1,charindex(')',dwgmc)-charindex('(',dwgmc)-1)
else '无' end as dwgmc
from entity_index
create table tb20111110(dwgmc varchar(60))
insert into tb20111110
select '(aa)011-钢梯T6-091(0600-8022-501)[td-tl]{asdfsdf}' union all
select '011-钢梯T6-091(0600-8022-505)[td-tl213]{asdfsdf}' union all
select '011-钢梯T6-091[td-tl1231]{asdfsdf}' union all
select '011-钢梯T6-091(0600-8022-501)[td-tl123]{asdfsdf}'
select
case when charindex('(',dwgmc)>0 and
charindex(')',dwgmc)>0
then
substring(dwgmc,charindex('(',dwgmc)+1,charindex(')',dwgmc)-charindex('(',dwgmc)-1)
else '无' end as dwgmc
from tb20111110
/*
dwgmc
------------------------
aa
0600-8022-505
无
0600-8022-501
(4 行受影响)
*/
alter table tb20111110 alter column dwgmc nvarchar(60) collate chinese_prc_ci_as_ws
select
case when charindex('(',dwgmc)>0 and
charindex(')',dwgmc)>0
then
substring(dwgmc,charindex('(',dwgmc)+1,charindex(')',dwgmc)-charindex('(',dwgmc)-1)
else '无' end as dwgmc
from tb20111110
/*
dwgmc
------------------------
0600-8022-501
0600-8022-505
无
0600-8022-501
(4 行受影响)
*/