Replace value from

  • Hi guys

    I hope everything's ok with all of you

    I have this example to do it in SQL. I want all characters after the space between. like this

    apagar

    Please beware the number of characters. The ideal would be something like "give all characters from space to the right"

    I hope I was clear.

    Thanks a lot in advance

  • without giving you the code look at functions charindex, substring and reverse(use twice) - using these 3 you will be able to get that split

  • Ok Frederico. I got this

    SUBSTRING (Restaurant, CHARINDEX ('- ', Restaurant), 20) AS Restaurant

    The result is this

    - RestaurantName

    I don't think I understood that REVERSE function because from what I've seen the result would be

    emaNtnaruatseR -

    I'm missing something, right?

  • you missing the fact I said reverse (use twice)

  • frederico_fonseca wrote:

    without giving you the code look at functions charindex, substring and reverse(use twice) - using these 3 you will be able to get that split

    I believe that reverse is relatively expensive.  I think it's simpler--and therefore easier--to use just charindex and stuff.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • multiple ways - I was just giving one of the ways with an incentive to search for it and try to understand it.

    and... substring vs stuff - which one is better performance? not sure if there are any testcases on that.

    one thing that may mess up with Any of the ways is if there are cases where the string has more than 1 space (e.g. 3 words or more).

    What should happen on that case Pedro

  • pedroccamara wrote:

    Hi guys I hope everything's ok with all of you

    I have this example to do it in SQL. I want all characters after the space between. like this apagar

    Please beware the number of characters. The ideal would be something like "give all characters from space to the right" I hope I was clear. Thanks a lot in advance

    Two things...

    1. What do you want to do if there's only one word (and, yes, it will happen)?  What about more than 2 words?
    2. You'd probably get some coded answers if you'd post some readily consumable data instead a a graphic.  See the article at the first link in my signature below for more information on the WHY and the HOW.

    --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)

  • Ah, I see, test driven development:

    ;with cte as
    (
    select * from (values ('Restaurant'),
    ('Restaurant Abc1'),
    ('Rest Abc'),
    ('Re Abc32'),
    ('Restau Abc4'),
    ('Restaurant Abbe'))T(C)
    )
    select C , right(C, len(C) - charindex(' ', C))
    from cte
  • Hi Frederico,

    I haven't missed that fact of using twice the REVERSE function but look at what I've said:

    I wrote this: SUBSTRING (Restaurant, CHARINDEX ('- ', Restaurant), 20) AS Restaurant

    and what I got was this

    '- RestaurantName'

    Even If I've used the REVERSE twice it wouldn't change what I'm looking at here...unless it should be used differently. And that's the help I ask you because I don't have enough knowledge to use that function properly.

    Also, to answer your question, it's always just 2 words separated by a space dash space, like my example

    Jonathan, I'm going to try your suggestion. I don't think it will work because it should be splited after ' - ' always and show me everything after that space dash space

    Thanks a lot all of you for helping me

    Still looking forward for the right solution

  • Using Jonathan's test data, here is the STUFF() version.

    WITH cte
    AS (SELECT *
    FROM
    (
    VALUES
    ('Restaurant')
    ,('Restaurant Abc1')
    ,('Rest Abc')
    ,('Re Abc32')
    ,('Restau Abc4')
    ,('Restaurant Abbe')
    ) T(C) )
    SELECT cte.C
    ,STUFF (cte.C, 1, CHARINDEX (' ', cte.C), '')
    FROM cte;

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • pedroccamara wrote:

    Jonathan, I'm going to try your suggestion. I don't think it will work because it should be splited after ' - ' always and show me everything after that space dash space

    Your example data does not include any hyphens. Please give us examples of what it really looks like.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Hi guys,

    I've just found the right solution based on Jonathan's idea

    Restaurant , RIGHT(Restaurant, len(Restaurant) - charindex(' - ', Restaurant)-2) AS C

    Thank you all so much, really

Viewing 12 posts - 1 through 11 (of 11 total)

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