44.1.0-053HGH-01
4.1.0-054HGH-01
4.1.0-14CL3-01
4.1.0-14L3-04
4.1.0-15YGH-01
4.1.0-15YGH-2
4.1.0-16YGH
4.1.0-7GGH
4.1.0-8YGH-1
4.1.0-8YGH-2
4.1.1-001-002
4.1.1-001-003
4.1.1-001-004
4.1.1-001-005在数据库中有一列的值如上,现想要实现两个横线间那段数字不足三位数的用0补充,使其满足三位数字,有什么办法。比如说:4.1.0-14CL3-01 改为4.1.0-014CL3-01   4.1.0-7GGH改为4.1.0-007GGH

解决方案 »

  1.   

    --> 测试数据: #A
    if object_id('tempdb.dbo.#A') is not null drop table #A
    go
    create table #A (A varchar(16))
    insert into #A
    select '44.1.0-053HGH-01' union all
    select '4.1.0-054HGH-01' union all
    select '4.1.0-14CL3-01' union all
    select '4.1.0-14L3-04' union all
    select '4.1.0-15YGH-01' union all
    select '4.1.0-15YGH-2' union all
    select '4.1.0-16YGH' union all
    select '4.1.0-7GGH' union all
    select '4.1.0-8YGH-1' union all
    select '4.1.0-8YGH-2' union all
    select '4.1.1-001-002' union all
    select '4.1.1-001-003' union all
    select '4.1.1-001-004' union all
    select '4.1.1-001-005'select RIGHT('0000'+SUBSTRING(SUBSTRING(A,CHARINDEX('-',A)+1,CHARINDEX('-',A+'-',CHARINDEX('-',A+'-')+1)-1-CHARINDEX('-',A+'-')),1,PATINDEX('%[^0-9]%',SUBSTRING(A,CHARINDEX('-',A)+1,CHARINDEX('-',A+'-',CHARINDEX('-',A+'-')+1)-1-CHARINDEX('-',A+'-'))+'A')-1),3) from #A
    /*       
    ------ 
    053
    054
    014
    014
    015
    015
    016
    007
    008
    008
    001
    001
    001
    001(所影响的行数为 14 行)
      

  2.   

    这么多CHARINDEX,头晕要是在接上原来的字符串还要加多少个CHARINDEX?
      

  3.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(data varchar(50))
    insert into #
    select '44.1.0-053HGH-01' union all
    select '4.1.0-054HGH-01' union all
    select '4.1.0-14CL3-01' union all
    select '4.1.0-14L3-04' union all
    select '4.1.0-15YGH-01' union all
    select '4.1.0-15YGH-2' union all
    select '4.1.0-16YGH' union all
    select '4.1.0-7GGH' union all
    select '4.1.0-8YGH-1' union all
    select '4.1.0-8YGH-2' union all
    select '4.1.1-001-002' union all
    select '4.1.1-001-003' union all
    select '4.1.1-001-004' union all
    select '4.1.1-001-005'update # set data = left(data, charindex('-', data)) + 
    right('000' + left(right(data, len(data)-charindex('-', data)), patindex('%[^0-9]%', right(data, len(data)-charindex('-', data)))-1), 3) +
    right(right(data, len(data)-charindex('-', data)), len(right(data, len(data)-charindex('-', data))) - patindex('%[^0-9]%', right(data, len(data)-charindex('-', data)))+1)select * from #/*
    data
    ----------------
    44.1.0-053HGH-01
    4.1.0-054HGH-01
    4.1.0-014CL3-01
    4.1.0-014L3-04
    4.1.0-015YGH-01
    4.1.0-015YGH-2
    4.1.0-016YGH
    4.1.0-007GGH
    4.1.0-008YGH-1
    4.1.0-008YGH-2
    4.1.1-001-002
    4.1.1-001-003
    4.1.1-001-004
    4.1.1-001-005
    */
      

  4.   


    不用update的话,用子查询可以简化很多--> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(data varchar(50))
    insert into #
    select '44.1.0-053HGH-01' union all
    select '4.1.0-054HGH-01' union all
    select '4.1.0-14CL3-01' union all
    select '4.1.0-14L3-04' union all
    select '4.1.0-15YGH-01' union all
    select '4.1.0-15YGH-2' union all
    select '4.1.0-16YGH' union all
    select '4.1.0-7GGH' union all
    select '4.1.0-8YGH-1' union all
    select '4.1.0-8YGH-2' union all
    select '4.1.1-001-002' union all
    select '4.1.1-001-003' union all
    select '4.1.1-001-004' union all
    select '4.1.1-001-005'select data = l +
    right('000'+left(r, patindex('%[^0-9]%',r)-1), 3) + 
    right(r, len(r)-patindex('%[^0-9]%',r)+1)
    from
    (
    select l = left(data, charindex('-',data)), r = right(data, len(data)-charindex('-',data)) from #
    ) t/*
    data
    ----------------
    44.1.0-053HGH-01
    4.1.0-054HGH-01
    4.1.0-014CL3-01
    4.1.0-014L3-04
    4.1.0-015YGH-01
    4.1.0-015YGH-2
    4.1.0-016YGH
    4.1.0-007GGH
    4.1.0-008YGH-1
    4.1.0-008YGH-2
    4.1.1-001-002
    4.1.1-001-003
    4.1.1-001-004
    4.1.1-001-005
    */
      

  5.   

    select case when patindex('%[^0-9.-]%',A)-1-charindex('-',A)<0 then A
    else 
    left(A,charindex('-',A))
      +right(ltrim(1000)+substring(A,charindex('-',A)+1, patindex('%[^0-9.-]%',A)-1-charindex('-',A)),3) 
      +right(A,len(A)-patindex('%[^0-9.-]%',A)+1)
    end
    from #a
      

  6.   

    left,right,substring,charindex,patindex  函数