Solved

sql update query help

Posted on 2014-03-26
3
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

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:
Scott Pletcher 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Index and Stats Management-Specific tables 8 50
MSSQL Convert Char to Date Time 5 37
SQL Server Serialization error 8 48
How to add an Index to a date/time field? 15 43
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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