现在我有一宗地号格式如下:
4-(1)-3
4-(2)-3
4-(3)-3
4-(4)-3
4-(10)-3
4-(11)-3
4-(11)-39需要修改为的格式是:
0040010030000   也就是说-前面的补足3位 中间为3位 其中中间的数字最多为2位,后面先补足3位 再加0至7位,后面的数字最多也只有2位  我自己想了一些方法 感觉都不高效 希望大家来探讨下

解决方案 »

  1.   

    汗  都写出来下
    用update来写下 我测试下 
      

  2.   

    --> 测试数据: @s
    declare @s table (num varchar(9))
    insert into @s
    select '4-(1)-3' union all
    select '4-(2)-3' union all
    select '4-(3)-3' union all
    select '4-(4)-3' union all
    select '4-(10)-3' union all
    select '4-(11)-3' union all
    select '4-(11)-39'select num=right('000'+parsename(num,3),3)+right('000'+parsename(num,2),2)+right('000'+parsename(num,1)+'0000',7)
    from(select num=replace(replace(replace(num,')',''),'(',''),'-','.') from @s)a
      

  3.   

    --> 测试数据: @s
    declare @s table (num varchar(9))
    insert into @s
    select '4-(1)-3' union all
    select '4-(2)-3' union all
    select '4-(3)-3' union all
    select '4-(4)-3' union all
    select '4-(10)-3' union all
    select '4-(11)-3' union all
    select '4-(11)-39'select num=right('000'+parsename(num,3),3)+right('000'+parsename(num,2),3)+right('000'+parsename(num,1)+'0000',7)
    from(select num=replace(replace(replace(num,')',''),'(',''),'-','.') from @s)a
    --结果:
    num
    -------------
    0040010030000
    0040020030000
    0040030030000
    0040040030000
    0040100030000
    0040110030000
    0040110390000
      

  4.   

    用update 谢谢 免得我还去改一下 挖哈哈 
      

  5.   

    SELECT 
    REPLACE(REPLACE(STRING,'-(','.'),')-','.') FROM TBTESTPT哥的可以直接这样
      

  6.   


    还可以这样
    SELECT '00'+REPLACE(REPLACE(STRING,'-(','.00'),')-','.00') FROM TBTEST
      

  7.   


    DECLARE @t TABLE(s varchar(10))INSERT INTO @t 
    SELECT '4-(1)-3' UNION ALL
    SELECT '4-(2)-3' UNION ALL
    SELECT '4-(3)-3' UNION ALL
    SELECT '4-(4)-3' UNION ALL
    SELECT '4-(10)-3' UNION ALL
    SELECT '4-(11)-3' UNION ALL
    SELECT '4-(11)-39' SELECT 
           SUBSTRING(s,1,CHARINDEX('-',s)-1),
           SUBSTRING(s,CHARINDEX('-(',s)+2,CHARINDEX(')-',s)-CHARINDEX('-(',s)-2),
           RIGHT(s,LEN(s)-CHARINDEX(')-',s)-1),       RIGHT('000'+SUBSTRING(s,1,CHARINDEX('-',s)-1),3),
           RIGHT('000'+SUBSTRING(s,CHARINDEX('-(',s)+2,CHARINDEX(')-',s)-CHARINDEX('-(',s)-2),3),
           LEFT(RIGHT('000'+RIGHT(s,LEN(s)-CHARINDEX(')-',s)-1),3)+'0000000',7)
    FROM   @t