一个字段是这样的
item
I1234,I4567
S346,S324
SD34
J4545我要查询出来的结果是
I1234
S346
SD34
J4545
就是说把那些有逗号的,查询出逗号前面的内容,没有都好的就是他本身,我用的方法是 用了两次select 然后union ,但是我觉得这样效率不是很好,有没有一个select就可以解决的呢

解决方案 »

  1.   

    declare @t table(item varchar(20))
    insert @t
    select 'I1234,I4567' union all
    select 'S346,S324' union all
    select 'SD34' union all
    select 'J4545'select reverse(stuff(reverse(item),1,charindex(',',reverse(item)),''))
    from @t/*结果
    I1234
    S346
    SD34
    J4545
    */
      

  2.   

    declare @t table(item varchar(20))
    insert @t
    select 'I1234,I4567' union all
    select 'S346,S324' union all
    select 'SD34' union all
    select 'J4545'select case when charindex(',',item)=0 then item else left(item,charindex(',',item)-1) end 
    from @T
      

  3.   

    --楼上的如果数据是下面的你查询的结果就不对declare @t table(item varchar(20))
    insert @t
    select 'I1234,I4567,asdfasdf' union all
    select 'S346,S324,asdfdf' union all
    select 'SD34' union all
    select 'J4545'
    select reverse(stuff(reverse(item),1,charindex(',',reverse(item)),''))
    from @t/*  你的结果,这样就不对了
    I1234,I4567
    S346,S324
    SD34
    J4545(4 row(s) affected)
    */