How to replace only the first specified character in a string and leave other occurrence of the character alone.

Hello my Friends:
I have a string that looks like this in a column:
Hello, World, Hello, Planet
I want to replace only the first occurrence so that the result looks like this:
Hello World, Hello, Planet
Working on some bad data source.
I need to replace the character regardless of it's location as long as it is the first.
I tried something like this, but I soon discovered that if the comma or character is missing the the string may get truncated or altered. If there are no characters in the string, then I want to simply leave it alone.
Here is where I am at:
DECLARE @MyValue NVARCHAR(MAX) = 'Hello, World, Hello, Planet';
SELECT MyString = STUFF('Hello, World', CHARINDEX(',', @MyValue, 0), 1, ' ');
Thanks my friends.

I have a string that looks like this in a column:
Hello, World, Hello, Planet
Why doesn't it say Hello Kitty?
Oh, sorry about that. Anyway:
SELECT MyString = CASE WHEN CHARINDEX(',', @MyValue, 0) >= 1
                       THEN STUFF(MyString, CHARINDEX(',', @MyValue, 0), 1, ' ')
                       ELSE MyString
                 END;
Erland Sommarskog, SQL Server MVP, [email protected]
Erland, love you sense of humor!! :)
Thanks for the solution.

Similar Messages

Maybe you are looking for

  • Running Logic in 64-bit but what about Snow Loepard?

    Hi all, I am wondering what the benefit is of running Logic in 64-bit if you only run Snow Leopard in 32-bit. Is there better performance when you run both Logic and the OS in 64-bit?

  • Bouncing .mac mail off webmail

    I'm using a company email address (@pillensfarm.com) as my main email but its linked to my .mac mail via a webmail service. I've made no changes to my account and now mac mail is suddenly not being forwarded from the webmail service. I'm receiving my

  • Tethered Capture issue, LR 5 and Canon 7D Mk II

    When I connect the camera and start TC I get the connect message and then a no camera detected, flashing back and forth about once a second -- regularly.  LR/ACR is up to date and reads and downloads the files fine.  Same thing on 2 computers.  Wonde

  • SD Revenue Recognition-Account settings for deferring COGS

    Hi Experts In the current process, COGS, cost of goods sold is getting posted into the COGS G/L account, while creation of billing document. In VF44 transaction, we find option for VPRS cost being captured against every line item & are available for

  • Resource Bundle question

    Can I keep HTML format in a resource bundle -properties file? Anybody has expirience?