Solved

Sql update statement

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now