这个sql语句将用在vc中执行的,所以加入程序能帮什么忙的话也可以!

解决方案 »

  1.   

    select (case when charindex(id,'.')>0 then left(id,1)+'-'+convert(varchar(50),convert(int,right(id,3))) else id end) from mytable
      

  2.   

    select left(id,(charindex('.',id)-1))+'-'+cast(cast(right(id,len(id)-charindex('.',id)) as int) as varchar(100)) from tablename 
    where charindex('.',id)>0
      

  3.   

    select (case when charindex(id,'.')>0 then left(id,charindex(id,'.')-1)
    +'-'+ltrim(rtrim(convert(varchar(50),convert(int,right(id,3))))) else id end) from MyTable
      

  4.   

    感谢 sdhdy(大江东去...) ,另外wzh1215(四脚蛇) 和txlicenhe(马可)你们的语句没有效果,可能没有考虑到象1和1.001的区别吧,不过也要表示感谢
      

  5.   

    但是sdhdy(大江东去...) ,我需要的是把id中的值直接变换成改变后的格式,而不是把它select出来,是不是应该加replace呢?该怎么加?
      

  6.   

    测试:create table mytable(id varchar(50))
    insert into mytable values('1')
    insert into mytable values('2')
    insert into mytable values('1.001')
    insert into mytable values('1.002')
    insert into mytable values('2.011')
    insert into mytable values('2.012')
    insert into mytable values('2.121')
    select (case when charindex('.',id)>0 then (left(id,1)+'-'+convert(varchar(50),convert(int,right(id,3)))) else id end)
    from mytable
    结果:
    1
    2
    1-1
    1-2
    2-11
    2-12
    2-121(7 row(s) affected)
      

  7.   

    这一次对了,那么我上面说的那个问题怎么解决呢?是不是要用到update?
      

  8.   

    update tablename  set id= left(id,(charindex('.',id)-1))+'-'+cast(cast(right(id,len(id)-charindex('.',id)) as int) as varchar(100)) 
    where charindex('.',id)>0
      

  9.   

    这样更好:create table #mytable(id varchar(50))
    insert into #mytable values('1')
    insert into #mytable values('2')
    insert into #mytable values('1.001')
    insert into #mytable values('1.002')
    insert into #mytable values('2.011')
    insert into #mytable values('2.012')
    insert into #mytable values('2.121')
    select case when charindex('.',id)>0 then left(id,charindex('.',id)-1)+'-'+convert(varchar(50),convert(int,substring(id,charindex('.',id)+1,8000))) else id end
    from #mytable
      

  10.   

    你这样就可以了:update mytable set id=case when charindex('.',id)>0 then left(id,charindex('.',id)-1)+'-'+convert(varchar(50),convert(int,substring(id,charindex('.',id)+1,8000))) else id end
      

  11.   

    select id,
    charindex('.',id) 小数点位置,
    (case 
      when charindex('.',id)>0--如果有小数点 
      then (left(id,charindex('.',id)-1)+
    '-'+
    convert(varchar(50),convert(int,right(id,len(id)-charindex('.',id))))

      else id 
    end)
    from mytable
      

  12.   

    update mytable set id=(select (case when charindex('.',id)>0 then (left(id,1)+'-'+convert(varchar(50),convert(int,right(id,3)))) else id end))select * from mytable
    结果:id                                                 
    -------------------------------------------------- 
    1
    2
    1-1
    1-2
    2-11
    2-12
    2-121(7 row(s) affected)
      

  13.   

    再一次感谢sdhdy(大江东去...) 以及其他几位朋友,pengdali(大力 V3.0) 执行了你的语句后还有有什么8000的限制