Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Updating first three characters Expand / Collapse
Author
Message
Posted Tuesday, June 26, 2012 3:21 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, May 10, 2013 12:01 PM
Points: 620, Visits: 1,544
I have a table with a field in it called branchcodes.

My problem is the branchcodes are wrong, but only the first three characters are wrong.

So, I need to update the branchcodes, but only change the first three characters, leaving the trailing three characters.

How do I do this?



Post #1321567
Posted Tuesday, June 26, 2012 3:22 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:22 AM
Points: 21,588, Visits: 27,384
Depends on what's wrong and how they need to be changed. Just from your vague description, not a clue on how I would proceed.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1321568
Posted Tuesday, June 26, 2012 3:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:21 PM
Points: 32,893, Visits: 26,765
krypto69 (6/26/2012)
I have a table with a field in it called branchcodes.

My problem is the branchcodes are wrong, but only the first three characters are wrong.

So, I need to update the branchcodes, but only change the first three characters, leaving the trailing three characters.

How do I do this?


Lookup "STUFF" in BOL. It's a function that works very well for this.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1321574
Posted Wednesday, June 27, 2012 3:27 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, May 10, 2013 12:01 PM
Points: 620, Visits: 1,544
Exactly what I needed.

Thanks Jeff.



Post #1322173
Posted Thursday, June 28, 2012 8:10 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:23 AM
Points: 324, Visits: 531
you can probably achieve what you need with a combination of CASE, LEFT and SUBSTRING. but it depends on the details of your requirement. it might be as simple as

UPDATE tblFoo
SET cFoo1 = 'ABC' + SUBSTRING(cFoo1,4,3)
WHERE LEFT(cFoo1,3) = 'ACB'

but if the first 3 letters need to be different depending on the current 3 letters it will be more complex eg

UPDATE tblFoo
SET cFoo1 = CASE LEFT(cFoo1,3)
WHEN 'ACB' THEN 'ABC' + SUBSTRING(cFoo1,4,3)
WHEN 'DFE' THEN 'DEF' + SUBSTRING(cFoo1,4,3)
END
WHERE LEFT(cFoo1,3) NOT IN ('ABC','DEF')



Ben

^ Thats me!


----------------------------------------
01010111011010000110000101110100 01100001 0110001101101111011011010111000001101100011001010111010001100101 01110100011010010110110101100101 011101110110000101110011011101000110010101110010
----------------------------------------
Post #1322491
Posted Thursday, June 28, 2012 8:24 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, March 30, 2013 9:39 AM
Points: 261, Visits: 966
It al depends on what your changing these values to, are your changes to the first 3 characters going to be the same for every row? if so then perhaps something like this


update table set column= replace(column,left(column,3),'')




*************************************************************

The first is always the hardest
Post #1322499
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse