SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UPDATE PART OF DATA


UPDATE PART OF DATA

Author
Message
Neel 7777
Neel 7777
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 84
TABLE IS LIKE

COMCOD ACTCODE ACTDESC
3306 180001 ADVANCE TO STAFF
3306 180002 ADVANCE TO OTHERS
3306 180003 ADVANCE TO SITE OFFICE


I LIKE TO CHANGE THREE DIGIT FROM THE OF ACTCODE TO 190 INSTEAD OF 180 WHERE EVERY ACTCODE HAVING 180 IN THE LEFT.

COMCOD nchar(4), ACTCODE PRIMARY KEY nvarchar(6), ACTDESC nvarchar(250)
bitbucket-25253
bitbucket-25253
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9431 Visits: 25280
If I understand what you desire to do correctly, this should do the job.
CREATE TABLE T(COMCOD nchar(4), ACTCODE nvarchar(6), ACTDESC nvarchar(250),
PRIMARY KEY (ACTCODE))

INSERT INTO T
SELECT '3306', '180001','ADVANCE TO STAFF' UNION ALL
SELECT '3306', '180002','ADVANCE TO OTHERS' UNION ALL
SELECT '3306', '180003','ADVANCE TO SITE OFFICE'

UPDATE T SET ACTCODE = '190' + SUBSTRING(ACTCODE,4,3)
WHERE SUBSTRING(ACTCODE,1,3) = '180'

Results:
COMCOD ACTCODE ACTDESC
3306 190001 ADVANCE TO STAFF
3306 190002 ADVANCE TO OTHERS
3306 190003 ADVANCE TO SITE OFFICE



Remember to test, and then test again before using in a Production database


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Cadavre
Cadavre
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4834 Visits: 8481
bitbucket-25253 (3/11/2013)
CREATE TABLE T(COMCOD nchar(4), ACTCODE nvarchar(6), ACTDESC nvarchar(250),
PRIMARY KEY (ACTCODE))

INSERT INTO T
SELECT '3306', '180001','ADVANCE TO STAFF' UNION ALL
SELECT '3306', '180002','ADVANCE TO OTHERS' UNION ALL
SELECT '3306', '180003','ADVANCE TO SITE OFFICE'

UPDATE T SET ACTCODE = '190' + SUBSTRING(ACTCODE,4,3)
WHERE SUBSTRING(ACTCODE,1,3) = '180'

Results:
COMCOD ACTCODE ACTDESC
3306 190001 ADVANCE TO STAFF
3306 190002 ADVANCE TO OTHERS
3306 190003 ADVANCE TO SITE OFFICE



Wouldn't STUFF be a better choice? You know you're replacing the first 3 characters, you don't know that the length of ACTCODE is always the same.

UPDATE T
SET ACTCODE = STUFF(ACTCODE, 1, 3, '190')
WHERE ACTCODE LIKE '180%';




Forever trying to learn

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

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search