Replacing a string when the value is found in a table

  • Hi guys,

    I'm trying to create a function where I can pass a string value that will be used for searching a table column and see if there is match, then replace that string partially when found like below:

    Passed string: "John Doe University"

    The column values from the table:

    SELECT LastName FROM ThisTable

    --------------------------------

    LastName

    ---------

    Cruz

    Patar

    Doe

    QuiJong

    Halal

    So if the search is succesful, I'll replace the string with empty string, then the value will be returned as:

    John University

    Hope it makes sense?

    Thanks

  • select @SearchString = REPLACE(@SearchString, LastName, '')

    FROM ThisTable

    select @SearchString

    _____________
    Code for TallyGenerator

  • Not exactly clear on what you are trying to accomplish, but my first instinct is to suggest the STUFF function. You can google it.

     

  • Thanks guys, Sergiy's answer is exactly what I needed.

    Have a nice day.:-)

  • Sergiy (5/2/2013)


    select @SearchString = REPLACE(@SearchString, LastName, '')

    FROM ThisTable

    select @SearchString

    +1 🙂

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 5 posts - 1 through 4 (of 4 total)

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