用case when 就行了

解决方案 »

  1.   

    select replace('id,cid,fid,id,aa,id','id','xx')
      

  2.   

    --假设你是用变量存储字符串
    declare @a varchar(1000)
    set @a='id,cid,fid,id,aa,id'
    select replace(replace(replace(replace(''''+@a+'''',',',''','''),'''id''','''xx'''),''',''',','),'''','')
    -------------------------------------------------------------------
    xx,cid,fid,xx,aa,xx
    假设你是表里的字段要替换
    update 表 set 某字段=replace(replace(replace(replace(''''+某字段+'''',',',''','''),'''id''','''xx'''),''',''',','),'''','')注意replace对ntext类型无效.
      

  3.   

    what 's mean of 'id不是固定的 '
      

  4.   

    1.若字符串a的位置是固定的,直接写a="xx,cid,fid,xx,aa,xx",呵呵
    2.若字符串a的位置是不固定的,
    create procedure sp1 @a varchar(100),@b varchar(100)
    as 
      select replace(replace(replace (' '+@a+' ',' '+@b+',','xx,'),','+@b+' ',',xx'),','+@b+',',',xx,') 
    goexec sp1 "id,cid,fid,id,aa,id","id"
    结果如上^-^
      

  5.   

    a='id,cid,fid,id,aa,id'
    replace(','+a+',',',id,',',xx,')
      

  6.   

    我觉得应该对id的左边是什么,或右边是什么的来判断是不是单独的ID
    用这种思路来做
      

  7.   

    一、“1.若字符串a的位置是固定的,直接写a="xx,cid,fid,xx,aa,xx",
    2.若字符串a的位置是不固定的,”
    纠正上面的说法,打得太快,粗心了,应该是id的位置二、“replace(replace(replace(' '+@a+' ',' '+@b+',','xx,'),','+@b+' ',',xx'),','+@b+',',',xx,')”
    上面的replace嵌套就是为了判断id是否是单独的,从而对其置换,我已经在SQL Server 2000上运行过了^-^