Replace certain text within row

Posted on 2011-10-26
Last Modified: 2012-05-12
I have a table with multiple rows like this:
if not exists (select * from dbo.sysusers where name = N'GA\ggalligan') Exec sp_grantdbaccess N'GA\GA\ggalligan', N'GA\ggalligan'

I need to remove the first GA\ where it says GA\GA\ggalligan.  All the rows are different lengths, so it always can't start in the same place. This needs to be an update statement.  Any help would be much appreciated.
Question by:tbailey2011
    LVL 1

    Expert Comment

    export rows to xls and use replace with function to change the text. Afterwards import back to SQL.


    Author Comment

    This is something that has to be automated everyday thru a sql query or that would work.
    LVL 68

    Accepted Solution

    UPDATE table
    SET col = REPLACE(col, ' N''GA\GA\', ' N''GA\')
    WHERE col LIKE '% N''GA\GA\%'
    LVL 15

    Expert Comment

    I would run this as a select to make sure it gives you the desired results first.

    UPDATE YourTable
    SET YourFieldName = STUFF(YourFieldName,CHARINDEX('GA\',YourFieldName),3,'')

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now