?
Solved

Update statement - using substring

Posted on 2007-04-07
12
Medium Priority
?
1,181 Views
Last Modified: 2008-01-09
I have an update statement that currently looks like this...

UPDATE    tbldata
SET              data = (SELECT     tblEx.[4KD]
                           FROM         tblEx INNER JOIN
                                                  tblDa ON tblEx.ID = SUBSTRING(tblDa.Data, 1, 3))

The question I have is that my data column looks like this - 101000ABC and I don't want to set the entire column equal to the new value. I only want to update a part of it using substring, but I dont know the syntax of how to do that here. I have tried SET SUBSTRING(data,4,3) = .... but that did not work.

Thanks.
0
Comment
Question by:jandhb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 39

Expert Comment

by:appari
ID: 18869623
try

UPDATE    tbldata
SET            data   = substring(data,1,3) + (SELECT     tblEx.[4KD]
                           FROM         tblEx INNER JOIN
                                                  tblDa ON tblEx.ID = SUBSTRING(tblDa.Data, 1, 3)) + substring(data,7,len(data)-7)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18869838
please try the following syntax:

UPDATE    tbldata
SET    data = tblEx.[4KD]
FROM  tblData        
INNER JOIN tblEx
  ON tblEx.ID = SUBSTRING(tblDa.Data, 1, 3))
0
 
LVL 1

Author Comment

by:jandhb
ID: 18869964
angelll, yours had a syntax error.
appari, yours worked, almost. :) Here is what is going on. It updated both records in tbldata. In other words, I have two records in tblData that look like this...

101000ABC
202000DEF
 
And then in tblEx I have one record right now with an ID of 101. So...in this particular case it should have only updated the record starting with 101. Thoughts?
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18870051
sorry, a ) too much at the end

UPDATE    tbldata
SET    data = tblEx.[4KD]
FROM  tblData        
INNER JOIN tblEx
  ON tblEx.ID = SUBSTRING(tblData.Data, 1, 3)
0
 
LVL 1

Author Comment

by:jandhb
ID: 18870966
angelll, that is not what I'm looking for. Running that query replaces the data completely, not just the characters 4,3.

appari query does this and works, except for the issue I mentioned before about it updating all records.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18871846
ok:

UPDATE    tbldata
SET    data   = substring(data,1,3) + tblEx.[4KD]  + substring(data,7,len(data)-7)
FROM  tblData        
INNER JOIN tblEx
  ON tblEx.ID = SUBSTRING(tblData.Data, 1, 3)
0
 
LVL 1

Author Comment

by:jandhb
ID: 18872923
angelll, that worked and I will award the points to you, but before I do may I ask one more question? How would update two parts of the string in tblData. Right now it is updating only the "x's" in this example here... 101XXXABC But what if I wanted to update it like this... 101XXXXXX Make sense?

In other words, I was trying something that like...

UPDATE    tbldata
SET    data   = substring(data,1,3) + tblEx.[4KDI]  + substring(data,7,len(data)-6),
SET    data   = substring(data,1,3) + tblEx.[STT]  + substring(data,7,len(data)-6)
FROM  tblData  
INNER JOIN tblEx
  ON tblEx.ID = SUBSTRING(tblData.Data, 1, 3)

But that did not quite work.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18872945
updating 2 parts is of course possible, but not sure what you mean exactly?
your explanation does not match the syntax try.
please retry to formulate the explanation...
0
 
LVL 1

Author Comment

by:jandhb
ID: 18872997
Currently in tblData my string looks like this...

101000ABC

At this point with your previous solution the "000" is being updated with the 4KD data in tblEx. I am now asking how you would also update the "ABC" data with the STT data in tblEx.

Is that clear?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 18873036
ok, so you mean this:
UPDATE    tbldata
SET    data   = substring(data,1,3) + tblEx.[4KDI]  + tblEx.[STT]  + substring(data,10,len(data)-9)
FROM  tblData  
INNER JOIN tblEx
  ON tblEx.ID = SUBSTRING(tblData.Data, 1, 3)
0
 
LVL 1

Author Comment

by:jandhb
ID: 18873791
that is exactly right.

may i ask you what this portion means - substring(data,10,len(data)-9)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18874588
that gets the characters from position 10 to the end...
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 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