declare @s Nvarchar(50)
set @s=N'|关键字1|关键字2|关键字3|'
select left(stuff(replace(@s,'|',','),1,1,''),len(stuff(replace(@s,'|',','),1,1,''))-1)
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
关键字1,关键字2,关键字3(影響 1 個資料列)
*/

解决方案 »

  1.   

    declare @s nvarchar(100)
    set @s='|关键字1|关键字2|关键字3|'select replace(substring(@s,2,len(@s)-2),'|',',')
    /*
    ----------------------------
    关键字1,关键字2,关键字3(1 行受影响)
    */
      

  2.   

    参考:--分解字符串包含的信息值去另外一表查询相应的信息
    (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)  2007-12-22  广东深圳)/*问题描述:
    需要将用户表中的用户组与信息表中的用户组对比.如果有相同的组则可查看该信息. 
    一个用户可能是几个用户组的成员.一条信息可能是几个用户组都可以查看. 表一 
    ID     usergroup     username 
    1      1,2,4         用户A 
    2      3             用户B 表二 
    id     usergroup     title   
    1      1,3,4         信息标题1 
    2      3             信息标题2 
    3      1,2,3         信息标题3 用户A可以看到   
    表二中的 
    信息标题1 
    信息标题3 用户B可以看到   
    表二中的 
    信息标题1 
    信息标题2 
    信息标题3
    */-------------------------------------------------------------
    --sql server 2000中用临时表的写法.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    go--建立一个辅助的临时表就可以了
    SELECT TOP 8000 id = identity(int,1,1) INTO tmp FROM syscolumns a, syscolumns b  
     
    select distinct c.username , d.title from
    (
      SELECT A.username,usergroup = SUBSTRING(A.usergroup, B.ID, CHARINDEX(',', A.usergroup + ',', B.ID) - B.ID) FROM tb A, tmp B WHERE SUBSTRING(',' + a.usergroup, B.id, 1) = ',' 
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA,tmp/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */--------------------------------------------------------------------
    --sql server 2000中不用临时表的SQL语句.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    goselect distinct c.username , d.title from
    (
      select a.username,usergroup=substring(a.usergroup,b.id,charindex(',',a.usergroup+',',b.id)-b.id)
      from tb a,(select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9  union select 10 union select 11 union select 12 union select 13 union select 14 union select 15) b
      where substring(','+a.usergroup,b.id,1)=','
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */-------------------------------------------------------------------
    --sql server 2005中的写法.
    create table TB(ID int,usergroup varchar(20),username varchar(10))
    insert into TB values(1,'1,2,4','用户A') 
    insert into TB values(2,'3'    ,'用户B')
    create table TA(id int,usergroup varchar(20),title varchar(10))
    insert into TA values(1,'1,3,4','信息标题1') 
    insert into TA values(2,'3'    ,'信息标题2') 
    insert into TA values(3,'1,2,3','信息标题3') 
    goselect distinct c.username , d.title from
    (
      SELECT A.username, B.usergroup FROM(SELECT username, [usergroup] = CONVERT(xml,'<root><v>' + REPLACE([usergroup], ',', '</v><v>') + '</v></root>') FROM TB)A
      OUTER APPLY(SELECT usergroup = N.v.value('.', 'varchar(100)') FROM A.[usergroup].nodes('/root/v') N(v))B
    ) c, TA d
    where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ',') > 0
    order by username , titledrop table TB,TA/*
    username   title
    ---------- ----------
    用户A        信息标题1
    用户A        信息标题3
    用户B        信息标题1
    用户B        信息标题2
    用户B        信息标题3(5 行受影响)
    */
      

  3.   


    declare @str nvarchar(100)
    set @str='|关键字1|关键字2|关键字3|'
    set @str=replace(substring(@str,2,len(@str)-2),'|',',')
    select @str
      

  4.   

    select keyword=replace(keyword,'|',',')
    from (
    select keyword=substring(keyword,2,len(keyword)-1
    from tb)A
      

  5.   

    SELECT SUBSTRING (REPLACE(keyword,'|',',') , 2 , len(keyword) -2 ) 
      

  6.   

    SELECT SUBSTRING (REPLACE(keyword,'|',',') , 2 , len(keyword) -2 ) 
      

  7.   


    declare @str nvarchar(100)
    set @str='|关键字1|关键字2|关键字3|'
    set @str=replace(substring(@str,2,len(@str)-2),'|',',')
    select @str
    /*
    结果:
    关键字1,关键字2,关键字3
    */