SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 replace last 3 characters of string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Steve2106
Posting Yak Master

United Kingdom
166 Posts

Posted - 08/16/2013 :  18:01:47  Show Profile  Reply with Quote
Hi There,

I need to change the last 3 characters of a string in a column of every record in my table.

Can someone tell me how I can do that.

Thanks for your help.

Best Regards,



Steve

ScottPletcher
Constraint Violating Yak Guru

USA
366 Posts

Posted - 08/16/2013 :  18:15:01  Show Profile  Reply with Quote
SELECT STUFF(column_name, LEN(column_name) - 2, 3, <new_chars_to_change_to>)

The new chars can be any number of bytes; it doesn't have to be 3.

Edited by - ScottPletcher on 08/16/2013 18:16:18
Go to Top of Page

Steve2106
Posting Yak Master

United Kingdom
166 Posts

Posted - 08/17/2013 :  16:48:35  Show Profile  Reply with Quote
Hi Scott,

Thanks for the reply.

That does what I need but it just gives me a list.
How do I actually overwright whats in the table with the list produced by your code.

Thanks again.

Best Regards,



Steve
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30218 Posts

Posted - 08/17/2013 :  17:25:06  Show Profile  Visit SwePeso's Homepage  Reply with Quote
UPDATE ?

UPDATE dbo.Table1
SET Col1 = SUBSTRING(Col1, 1, LEN(Col1) - 3) + 'new content';

Beware that LEN does not care about trailing spaces.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 08/18/2013 :  04:28:28  Show Profile  Reply with Quote

UPDATE dbo.Table1
SET Col1 = REPLACE(Col,RIGHT(Col,3),'NewContent')





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sivadss2007
Starting Member

India
18 Posts

Posted - 08/28/2013 :  07:39:23  Show Profile  Reply with Quote
UPDATE Table
set
column_name = replace(column_name right(column_name,3),'new value')


P.Siva
Go to Top of Page

lazerath
Constraint Violating Yak Guru

USA
328 Posts

Posted - 08/28/2013 :  11:27:36  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


UPDATE dbo.Table1
SET Col1 = REPLACE(Col,RIGHT(Col,3),'NewContent')




This method is VERY DANGEROUS as it will replace any other occurrence of that pattern with the 'NewContent'. As an example:


DECLARE @t table (col varchar(50));

INSERT @t values('010101010101010');
INSERT @t values('100100101010100');
INSERT @t values('abcdefabcefgabc');

SELECT * FROM @t;
/*
col
010101010101010
100100101010100
abcdefabcefgabc
*/

UPDATE @t
SET Col = REPLACE(Col,RIGHT(Col,3),'NewContent');

SELECT * FROM @t;

/*
col
NewContent1NewContent1NewContent1NewContent
NewContentNewContent101010NewContent
NewContentdefNewContentefgNewContent
--*/


The same goes for the solution provided by sivadss2007 (which is essentially the same).

Even if you were relatively confident your strings were unique, I would still follow either Scott or SwePeso's methods as it is far more exact and reliable.

Edited by - lazerath on 08/28/2013 11:30:29
Go to Top of Page

Steve2106
Posting Yak Master

United Kingdom
166 Posts

Posted - 08/28/2013 :  11:36:38  Show Profile  Reply with Quote
Hi There,

Thanks for the update.

I'll be careful with this.

Best Regards,

Steve.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000