Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating the left side of a string up to a delimiter

My column "ColumnOne" in my table "MyTable" has values like this: Delimiter is character ‘-‘

|Something                  |   
|Something - SomeOtherThing |
|Something - SomethingElse  |   
|Something - Whatever       |
|OtherThing -               |

I want to update the values so eventually it look like this:

|Something                  |   
|           SomeOtherThing  |
|           SomethingElse   |   
|           Whatever        |
|                           |

So basically algorithm being to replace with white space and keep going until you see ‘-‘ , replace that too also with whitespace.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried the REPLACE command to say like
UPDATE MyTable SET ColumnOne = REPLACE(ColumnOne, ' - ', ' ' + ColumnOne) but that’s wrong. I couldn’t figure out the pattern for its second argument.
Any suggestions are appreciated.

>Solution :

Use charindex to find the amount of characters to change, stuff to perform the change, and replicate to generate a string of N spaces. Try this:

stuff(ColumnOne,1,charindex('-',ColumnOne),replicate(' ',charindex('-',ColumnOne))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading