replace() remove the last character of some values

  • in my table I have values such as

    Admin -- Assistants

    Finance -- Accountant

    Underwriting

    I have code in the query to remove [value --] so its returned as

    Assistant

    Accountant

    Underwritin

    however, if the value doesn't have [--], its remove the last character of the value (as underwritin)

    am I missing something in this, or is there another way to remove the text and the dashes if it exists?

    REPLACE(SUBSTRING([family],

    CHARINDEX('--', [family]),

    LEN([family])), '--', '') AS [JobTitles]

  • The CHARINDEX gives you a 0 if there is no --

    When you use 0 as the start position for substring it messes it up. Here is the MSDN page on SUBSTRING.

    Take note of the following about START:

    Is an integer or bigint expression that specifies where the returned characters start. If start is less than 1, the returned expression will begin at the first character that is specified in expression. In this case, the number of characters that are returned is the largest value of either the sum of start + length- 1 or 0. If start is greater than the number of characters in the value expression, a zero-length expression is returned.

  • LEFT(family, CHARINDEX('--', [family] + '--') - 2)

    Edit: Added the "- 2".

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • What's wrong with just doing a replacement?

    UPDATE tgt

    SET Family = REPLACE(Family,'--','')

    FROM dbo.yourtable

    WHERE Family LIKE '%--%'

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (3/4/2015)


    What's wrong with just doing a replacement?

    UPDATE tgt

    SET Family = REPLACE(Family,'--','')

    FROM dbo.yourtable

    WHERE Family LIKE '%--%'

    ;

    there are other apps using the tables that I'm pulling from, so we can't do an update. It crossed my mind, but can't.

  • SQL_NuB (3/4/2015)


    Jeff Moden (3/4/2015)


    What's wrong with just doing a replacement?

    UPDATE tgt

    SET Family = REPLACE(Family,'--','')

    FROM dbo.yourtable

    WHERE Family LIKE '%--%'

    ;

    there are other apps using the tables that I'm pulling from, so we can't do an update. It crossed my mind, but can't.

    Yeah, I'd be extremely careful before just arbitrarily discarding potential delimiters in a column value.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • SQL_NuB (3/4/2015)


    Jeff Moden (3/4/2015)


    What's wrong with just doing a replacement?

    UPDATE tgt

    SET Family = REPLACE(Family,'--','')

    FROM dbo.yourtable

    WHERE Family LIKE '%--%'

    ;

    there are other apps using the tables that I'm pulling from, so we can't do an update. It crossed my mind, but can't.

    Ah, crud. I totally misread the original post. My apologies.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ScottPletcher (3/4/2015)


    LEFT(family, CHARINDEX('--', [family] + '--') - 2)

    Edit: Added the "- 2".

    when I add this, its removing the text before the [--].

  • Late to the party, but perhaps this does what you're looking for:

    WITH SampleData (s) AS

    (

    SELECT 'Admin -- Assistants'

    UNION ALL SELECT 'Finance -- Accountant'

    UNION ALL SELECT 'Underwriting'

    )

    SELECT s, REPLACE(STUFF(s, 1, CHARINDEX('--', s), ''), '- ', '')

    FROM SampleData;


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 9 posts - 1 through 8 (of 8 total)

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