Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Rtrim/Ltrim/Replace in string

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2011-11-02 : 09:25:22
I have a series of strings, all should end up with the same first 9 characters and the same last 3 characters. I'm struggling with the Replace / update syntax i need to use. For example, the first row currently reads thusly:
GCSO0010112930000ENF
What i need to do is insert "CIV" after the GCSO00, BUT i need to keep the 1011293 component and just drop three of the zeros before the ENF portion, so the end result should be:
GCSO00CIV10112930ENF
It's a varchar(20), so i have that constraint.
All other rows are similar in that the padding of zeros before the ENF portion need to sacrifice themselves to accomodate the CIV after the GCSO00.

Thank you for any help with this.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-11-02 : 09:33:03
...
replace(replace(column,'GCSO00','GCSO00CIV'),'000ENF','0ENF')
...
where column like '%000ENF'

edit: typo

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -