Solved

How to update "part of a text field" without changing the entire contents?

Posted on 2016-09-26
2
59 Views
Last Modified: 2016-09-26
Experts,

I have a table 'Table1' that has a Text Field that contains data like this:

RowID,DetailData
1,1P0014.241102.0000                          1053591503          5806909             1230                                  HP23

The 3rd set of numbers in my DetailData text column is my MemberID field which is '5806909' in the above example. I need to know how to update this MemberID WITHOUT changing the contents of the rest of the Text. For example, the NewMemberId is '6006100'.  

I need the change to be reflected like this:

RowID,DetailData
1,1P0014.241102.0000                          1053591503          6006100             1230                                  HP23

The "spacing" and character set cannot be changed at all.. but only the 3rd set of numbers which again is my MemberID.

How can I accomplish this within an UPDATE Script?

Thanks
0
Comment
Question by:MIKE
2 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41816970
If you have a specific byte location, use STUFF:

SELECT ..., STUFF(DetailData, CHARINDEX('5806909', DetailData), LEN('5806909'), '6006100'), ...
FROM table_name

Or:
UPDATE table_name
SET DetailData = STUFF(DetailData, CHARINDEX('5806909', DetailData), LEN('5806909'), '6006100')
WHERE ...
0
 
LVL 17

Author Closing Comment

by:MIKE
ID: 41816982
Thanks you!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LTrim & Double Space Correction 5 40
Check ALL SP in database make sure there are no errors 17 43
Re-appearing SQL Server Agent jobs 7 28
job schedule 8 16
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

808 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