Solved

sql update query help

Posted on 2014-03-26
3
249 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 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
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
Comment Utility
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
Comment Utility
perfect.  I prefer the inner join over the where clause join.

Thanks scott.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 87
TSQL - Rollup data set 7 29
Sql query for filter 12 21
Sql query 34 17
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

18 Experts available now in Live!

Get 1:1 Help Now