Solved

Sql update statement

Posted on 2014-11-05
3
180 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Insert to Begin if data exists 2 33
Nested Case statement 4 39
how to just get time from a date 6 33
(sql serv16)ssis 2016 question/check 1 73
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 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