Solved

sql update query help

Posted on 2014-03-26
3
251 Views
Last Modified: 2014-03-26
I am in need of help writing an update query.

I have a single table:  [companycitymaster]  
This table contains columns [state], [citycode] and [splc].

I need to update the state = 'QC' rows with the [splc] value where
state = 'PQ' BUT only if the state = 'QC' rows are blank and only where the [citycode] is equal.  

I need to do this update before i delete all the pq's.  

Thanks

See data below.

data
0
Comment
Question by:hougie40
3 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 39956550
E.g.

UPDATE  O
SET     SPLC = ( SELECT I.SPLC
                 FROM   CompanyCityMaster I
                 WHERE  I.CityCode = O.CityCode
                        AND I.[State] = 'PQ'
               )
FROM    CompanyCityMaster O
WHERE   O.[State] = 'QC'
        AND LEN(RTRIM(COALESCE(O.SPLC, ''))) = 0;

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39956726
UPDATE ccm_qc
SET splc = ccm_pq.splc
FROM (
    SELECT state, citycode, splc
    FROM companycitymaster
    WHERE
        state = 'QC' AND
        splc = ''
) AS ccm_qc
INNER JOIN (
    SELECT state, citycode, splc
    FROM companycitymaster
    WHERE
        state = 'PQ' AND
        splc > ''
) AS ccm_pq ON
    ccm_pq.citycode = ccm_qc.citycode
0
 

Author Closing Comment

by:hougie40
ID: 39956791
perfect.  I prefer the inner join over the where clause join.

Thanks scott.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scheduling Jobs for Execution: 4 23
SQL profiler equivalent in MS-Access 3 45
SQL Statement to Update Email Domain 2 23
SQL server is using more virtual memory. 5 68
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

19 Experts available now in Live!

Get 1:1 Help Now