Comma Seperators

  • I am trying to remove "," from strings .( Only if there is no string after the comma)

    example :

    abc ,

    cde,xyz,aaa,

    zzz, yyy ,ttt,

    I am expecting the output as

    abc

    cde,xyz,aaa

    zzz,yyy,ttt

    If I use Replace:

    Select

    Replace(ApplicationType ,',','')Application

    Then it simply removes all the ",". I believe there should be a case .. Any ideas?

  • This resolved the Issue

    case

    when right(rtrim(ApplicationType ),1) = ',' then substring(rtrim(ApplicationType ),1,len(rtrim(ApplicationType ))-1)

    Else ApplicationType

    End as Application

  • This will work as well:

    with SampleData as (

    select

    StrVal

    from

    (values ('abc ,'),('cde,xyz,aaa,'),('zzz, yyy ,ttt,'),('qwe'),('asd,fgh'))dt(StrVal)

    )

    select

    StrVal,

    left(StrVal,len(StrVal) - patindex(',%',reverse(StrVal)))

    from

    SampleData;

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply