Strip Unwanted Characters

  • Hello All

    Lets say I have a column with some "unwanted" characters on the end I might want to remove, how would you suggest I do this?

    Example:

    create table test

    (

    col varchar(10)

    )

    insert test(col)

    select 'testvalue**'

    union all

    select 'testvalue**'

    union all

    select 'testvalue***'

    I'd like to strip the asterisk values from the end?

    As you can see there is no telling how many asterisks you can have?

  • With the values you've given, LEFT() would do what you ask for.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • If it's always '*' that you want to remove, you could look at the REPLACE function.

  • Thanks for your responses.

    The only thing is I want to replace the character(s) only if it occurs at the end so 'opobo' strip away 'o' should become 'opob'

  • So how are you defining the characters you want to keep vs the characters you want to remove?

  • Try this:

    IF OBJECT_ID('test') > 0

    DROP TABLE test

    GO

    CREATE TABLE test (col VARCHAR(20))

    GO

    INSERT test

    (

    col

    )

    SELECT 'testvalue**'

    UNION ALL

    SELECT 'testvalue**'

    UNION ALL

    SELECT 'testvalue***'

    GO

    SELECT REVERSE(SUBSTRING(REVERSE(col), PATINDEX('%[^*]%', REVERSE(col)), 20))

    FROM test

    GO

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanks.

    This does the trick 🙂

Viewing 7 posts - 1 through 6 (of 6 total)

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