I am having a hard time to figure out how to remove the parentheses and anything inside it, plus the space in front. I tried the combination of CharIndex, Substring, and LEFT, but could not get it work correctly.
Here is the example. Let's say I have a very simple table "ContactName" and a column "DisplayName"
Query would be "Select DisplayName FROM ContactName".
In the ContactName table, the data looks like this:
Joe Blow (firstname.lastname@example.org)
John Deep (email@example.com)
(Tim.King@gmail.com) King, Timothy
To remove all characters between
) in the string (if they are present), you can use
stuff and then a combination of
rtrim to remove all the trailing and leading spaces.
case when charindex('(',displayname) > 0 and charindex(')',displayname) > 0 then rtrim(ltrim(stuff(displayname,charindex('(',displayname),charindex(')',displayname),''))) else displayname end