Solved

Sql update statement

Posted on 2014-11-05
3
179 Views
Last Modified: 2014-11-05
I have the following sql update statement that I need to run.  

I am not sure this is written correctly and was looking for some help.  

update binlocat
set LICENSE_PLATE = UPLOAD.FIELD034
where SUBSTRING(BINLOCAT.EXTENDED, 102, 8) = UPLOAD.FIELD021 and
                  (UPLOAD.FIELD001='st') AND (UPLOAD.FIELD003='05') AND
                   (UPLOAD.FIELD008 like '10pl%')
0
Comment
Question by:notasgoodasyou
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40424292
Since this UPDATE statement has two tables, binlocal and UPLOAD, then it needs a FROM and JOIN that spell out how the two tables are related.  Something like (replace id with the column that joins these two tables)...
update binlocat
set LICENSE_PLATE = UPLOAD.FIELD034
FROM binlocat
   JOIN UPLOAD ON binlocat.id = UPLOAD.id
where SUBSTRING(BINLOCAT.EXTENDED, 102, 8) = UPLOAD.FIELD021 and 
   (UPLOAD.FIELD001='st') AND 
   (UPLOAD.FIELD003='05') AND
   (UPLOAD.FIELD008 like '10pl%')

Open in new window

0
 

Author Comment

by:notasgoodasyou
ID: 40424305
Thanks Jim

Will the following work?


update binlocat
set LICENSE_PLATE = UPLOAD.FIELD034
FROM binlocat
   JOIN UPLOAD ON SUBSTRING(BINLOCAT.EXTENDED, 102, 8) = UPLOAD.FIELD021
where SUBSTRING(BINLOCAT.EXTENDED, 102, 8) = UPLOAD.FIELD021 and
   (UPLOAD.FIELD001='st') AND
   (UPLOAD.FIELD003='05') AND
   (UPLOAD.FIELD008 like '10pl%')
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40424342
Should work, although if you are joining on the SUBSTRING.. expression then you don't need the same expression in the WHERE clause.  

I don't have a connection to your data source, so I can't check for things like table and column names spelled correctly, data types, etc.

Check out the T-SQL below, and note the use of table aliases (b and u), indenting, and removed some unnecessary parentheses to make it much more readable.
update b
set b.LICENSE_PLATE = u.FIELD034
FROM binlocat b
   JOIN UPLOAD u ON SUBSTRING(b.EXTENDED, 102, 8) = u.FIELD021 
where
   u.FIELD001='st' AND 
   u.FIELD003='05' AND
   u.FIELD008 like '10pl%'

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

821 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